I'm studying the promises with Nodejs.
I've a question about a situation with two nested Q.all.
Q.all(promises1)
.then(function(res1) {
var promises2 = <METHOD THAT USE THE RES1>
Q.all(promises2)
.then(function(re2) {
...
})
})
The problem is that I need the result of the first Q.all for the second.
The promises1 are multiple save function, and I need the objectId of the item saved in the multiple function that I use in the promises2.
I'am studying the promises for not having nested function, my question is how can I resolve this nest?
You can return a promise from within a then function to continue a chain. Also I want to note that the built in Promise API is supported by node.
Promise.all(promises1)
.then(res => {
let promises2 = [];
return Promise.all(promises2);
})
.then(res => {
});
how can I resolve this nest?
As always :-) You need to return the promise from the then callback, in this case the one you got with the second Q.all call, and then you can chain your second then invocation to the outer promise:
Q.all(promises1)
.then(function(res1) {
var promises2 = … // method that uses `res1`
return Q.all(promises2)
// ^^^^^^
}).then(function(res2) {
…
});
Related
Js is not my first language, by a long chalk. I code in 5 or 6 others, and am comfortable with callbacks, which means that I should be able to grok promises.
However, I inherited a node.js project with something like this (simplified), which makes me uncertain)
let promise = someFunction.then({return xxxx;});
// lots of more code & promises
Promises.all().then(() => {return somethingElse});
I am comfortable with lots of promises and Promises.all() to wait for them all to resolve (although I am replacing all .then with await for cleaner code), BUT ...
that return inside a .then disturbs me. If I understand correctly, .then just wraps an async callback - which could happen at any time, even before the rest of the code and that Promises.all, leaving some of the code unexecuted.
Am I correct to be concerned, or is there something that I am missing?
Tl;dr - is it ok to return inside a .then?
Returning inside .then is extremely common and fine to do. What it does is it produces a Promise that now resolves to the value returned (rather than to the value returned by the previous Promise). Example:
// resolves to 1
const prom1 = Promise.resolve(1);
// chains off of prom1, resolves to 2
const prom2 = prom1.then(() => {
return 2;
});
prom1.then(console.log);
setTimeout(() => {
prom2.then(console.log);
});
This technique is very useful when you need to pass along a value from a prior .then to a subsequent .then.
const makeApiCall = num => Promise.resolve(num + 3);
Promise.resolve(1)
.then((result1) => {
return makeApiCall(result1);
})
.then((result2) => {
console.log(result2);
});
What the return will do in your situation specifically:
let promise = someFunction.then({return xxxx;});
// lots of more code & promises
Promises.all().then(() => {return somethingElse});
If promise is passed to the Promise.all, then its resolve value will be xxxx, rather than whatever someFunction resolves to. (If the Promise.all's .then ignores its argument, then it does nothing except wait for the returned value to resolve, if it's a Promise.)
const someFunction = () => Promise.resolve(1);
let promise = someFunction().then(() => {
return 'xxxx';
});
Promise.all([promise]).then((allResults) => {
console.log('allResults', allResults);
return 'somethingElse';
})
.then((result2) => {
console.log('Next `.then`', result2);
});
Note that your current code has a number of syntax issues you need to correct.
I am dealing with an old codebase and faced this situation where it's difficult for me to understand the order of execution after promises are resolved. I am more familiar with async/await syntax or with a chain of then-s, but not with this one. Here is the snippet:
_loadCaseDetail: funciton (arg1, arg2, arg3) {
var oDataModel = this.getOwnerComponent().getModel('db2');
loadLatestDatasetVersion(oDataModel).then(function (datasetVersion) {
// do something
});
loadCountries(oDataModel).then(function (countries) {
// do something
});
numberOfRulesetChanges(oDataModel).then(function (elements) {
// do something
});
return fireImplicitLock(caseUuid).then(function (lockData) {
// do something
}).then(function () {
// do something
})
}
loadLatestDatasetVersion, loadCountries, numberOfRulesetChanges, fireImplicitLock - all return promises
My question is: What would be the order in this case for all then-s that come after these promises?
Is it exactly sequential as it is or it's not and we can refactor it with say Promise.all?
Does it even need any refactoring?
What would be the order in this case for all then-s that come after these promises?
The then function will fire when the associated promise resolves. That's the point of asynchronous code. It goes away until it is ready to do the next thing.
we can refactor it with say Promise.all
You could use Promise.all to wait until multiple promises are resolved before doing anything with the resulting values, but that would be inefficient unless the then function for C requires data from A or B.
loadLatestDatasetVersion, loadCountries, numberOfRulesetChanges, fireImplicitLock - all return promises they will get inserted into Event loop one after another.
then part of promises will get executed once promises are resolved. Which promise's then will get executed depends upon the execution of the respective promise.
_loadCaseDetail: funciton (arg1, arg2, arg3) {
var oDataModel = this.getOwnerComponent().getModel('db2');
loadLatestDatasetVersion(oDataModel).then(function (datasetVersion) {
// do something
});
loadCountries(oDataModel).then(function (countries) {
// do something
});
numberOfRulesetChanges(oDataModel).then(function (elements) {
// do something
});
return fireImplicitLock(caseUuid).then(function (lockData) {
// do something
}).then(function () {
// do something
})
}
Promise.all is like a gate where you can pass array of promises and it will resolve only after all the promises are resolved.
let p1 = Promise.resolve(loadLatestDatasetVersion(oDataModel));
let p2 = Promise.resolve(loadCountries(oDataModel));
let p3 = Promise.resolve(numberOfRulesetChanges(oDataModel));
let p4 = Promise.resolve( fireImplicitLock(caseUuid)
let finalPromise = Promise.all([p1,p2,p3,p4]);
finalPromise.then(([datasetVersion,countries,elements,lockData])=>{
// do something here with datasetVersion, countries, elements, lockData you have all the params cause all the promises are resolved.
})
Same thing you can achieve using Promise.all like shown above.
More about promises
return fireImplicitLock(caseUuid).then(function (lockData) {
// do something
}).then(function () {
// do something
})
Above line will not return any result from promises, it will return Promise object with its status resolved/pending/rejected and value.
I'm trying to write a function that doesn't return it's value until a Promise inside the function has resolved. Here's a simplified example of what I'm trying to do.
'use strict';
function get(db, id) {
let p = db.command('GET', 'group:${id}');
p.then(g => {
g.memberNames = [];
g.members.forEach(id => {
User.get(db, id)
.then(profile => g.memberNames.push(profile.name))
.catch(err => reject(err));
});
return g;
});
}
It's a function that requests a group id and returns the data for that group. Along the way it's also throwing in the user's names into the data structure to display their names instead of their user id. My issue is that this is running asynchronously and will skip over the .then callbacks. By the time it gets to return g none of the callbacks have been called and g.memberNames is still empty. Is there a way to make the function wait to return g until all callbacks have been called?
I've seen a lot of stuff about await. Is this necessary here? It's highly undesired to add other libraries to my project.
Since your operation to return all the profile names is also async you should return a Promise fulfilled when all the other async operations complete (or reject when one of them is rejected) done with Promise.all
function get(db, id) {
let p = db.command('GET', 'group:${id}');
return p.then(g => {
return Promise.all(g.members.map(id => {
// NOTE: id is shadowing the outer function id parameter
return User.get(db, id).then(profile => profile.name)
})
})
}
Is there a way to make the function wait to return g until all callbacks have been called?
Yes, there is. But you should change your way of thinking from "wait until all the callbacks have been called" to "wait until all the promises are fulfilled". And indeed, the Promise.all function trivially does that - it takes an array of promises and returns a new promise that resolves with an array of the results.
In your case, it would be
function get(db, id) {
return db.command('GET', 'group:${id}').then(g => {
var promises = g.members.map(id => {
// ^^^ never use `forEach`
return User.get(db, id).then(profile => profile.name);
// ^^^^^^ a promise for the name of that profile
});
//
return Promise.all(promises).then(names => {
// ^^^^^^^^^^ fulfills with an array of the names
g.memberNames = names;
return g;
});
});
}
I've had a problem trying to wrap my mind around JavaScript promises. Here is my lately frustration:
var rp = require('request-promise');
function process1000() {
//gets 1000 objects from DB to process
get1000objects()
.then(function (docs) {
Promise.map(docs, addApiData)
})
.then(function (objects) {
console.log(objects)
})
}
function addApiData(element) {
return rp(url).then(function (res) {
resolve(res);
})
.catch(console.error);
}
When rp(url) is called in addApiData(), it doesn't wait until the function is resolved before beginning the next item in the array that I'm passing to Promise.map in process1000(). As soon as rp(url) is called in addApiData, addApiData is called on the next item in the array. The second .then() function in process1000 is then called, prematurely before everything has had a chance to resolve in Promise.map. I've tried a few different libraries to make my GET request but they're all having this same issue. What am I doing wrong here? Any help would be greatly appreciated.
I think you're looking for something more like this:
var rp = require('request-promise');
function process1000() {
//gets 1000 objects from DB to process
get1000objects()
.then(function (docs) {
return Promise.map(docs, addApiData)
})
.then(function (objects) {
console.log(objects);
})
}
function addApiData(element) {
return rp(url)
.catch(console.error);
}
The main point here is that you need to make sure you're returning the promises, or the next then won't know what to do with its value.
Also, try to avoid the Promise constructor antipattern.
In other words, if you return a promise, you don't need to return then as well.
These two are equivalent:
return rp(url).then(function(res) { return res; });
// and
return rp(url);
They both return a promise (rp(url) returns a promise, and rp(url).then( /* ... */) returns a promise).
I have created my first deferred object in Node.js using deferred module and it works great when I pass result to next function and trigger resolve and reject.How to chain execution of array of functions when every function returns deferred.promise ?
I have like input parameters array of functions and input parameter for first function and every next function get parameter from previous.
It works like f1(100).then(f2).then(f3), but how when I have n number of functions.
Same idea, but you may find it slightly classier or more compact:
funcs.reduce((prev, cur) => prev.then(cur), starting_promise);
If you have no specific starting_promise you want to use, just use Promise.resolve().
You need to build a promise chain in a loop:
var promise = funcs[0](input);
for (var i = 1; i < funcs.length; i++)
promise = promise.then(funcs[i]);
ES7 way in 2017. http://plnkr.co/edit/UP0rhD?p=preview
async function runPromisesInSequence(promises) {
for (let promise of promises) {
console.log(await promise());
}
}
This will execute the given functions sequentially(one by one), not in parallel.
The parameter promises is a collection of functions(NOT Promises), which return Promise.
Building on #torazaburo, we can also add an 'unhappy path'
funcs.reduce(function(prev, cur) {
return prev.then(cur).catch(cur().reject);
}, starting_promise);
ES6, allowing for additional arguments:
function chain(callbacks, initial, ...extraArgs) {
return callbacks.reduce((prev, next) => {
return prev.then((value) => next(value, ...extraArgs));
}, Promise.resolve(initial));
}
For possible empty funcs array:
var promise = $.promise(function(done) { done(); });
funcs.forEach(function(func) {
promise = promise.then(func);
});
If you're using ES7 and need something thenable and similar to Promise.all(), this seems to work.
Promise.resolve(
(async () => {
let results = []
for (let promise of promises) {
results.push(await promise)
}
return results
})()
)