I'm learning about JS promises and have made some progress in my understanding of things, however unsure how to bring it together with
return
Q.all
getParentsForLocation
function doBusiness() {
return Q.all(
locations.map(function(item, currentIndex) {
return getParentsForLocation(item.id)
.then(function(res) {
return checkParent(res, currentIndex)
}
});
}))
.then(_.uniq(locations))
}
locations
uniq
_.uniq(someArrayIHave);
Q.all([])
doBusiness()
Do i need to place this in a
Q.all(…)
?
Yes. Your map()
call should get you an array of promises.
If so, would it run each method in that array sequentially?
No. Or at least, we don't know, they could do anything on the inside.
I presume there is something I'd need to do with that
doBusiness()
function, e.g. return some promise
Yes. From my promise rules of thumb: If a function does something asynchronous, it must return a promise. This is also true for your two callback functions.
How would that look?
function doBusiness() {
return Q.all(locations.map(function(item, currentIndex) {
// ^^^^^^ ^^^^^^
return getParentsForLocation(item.id)
// ^^^^^^
.then(function(res) {
return updateDB(res, currentIndex);
// ^^^^^^
});
}));
// ^
}