admin管理员组文章数量:1415664
I'm trying to execute an asynchronous routine for a bunch of items in a list that I get from a database, but I'm having trouble understanding how promise.all works and what it does.
Here is the code I'm using right now:
/**
* Queues up price updates
*/
function updatePrices() {
console.log("~~~ Now updating all listing prices from Amazon API ~~~");
//Grabs the listings from the database, this part works fine
fetchListings().then(function(listings) {
//Creates an array of promises from my listing helper class
Promise.all(listings.map(function(listing){
//The promise that resolves to a response from the routine
return(listing_helper.listingPriceUpdateRoutine(listing.asin));
})).then(function(results){
//We want to log the result of all the routine responses
results.map(function(result){
console.log(result);
});
//Let us know everything finished
console.log("~~~ Listings updated ~~~");
}).catch(function(err){
console.log("Catch: ", err);
});
});
}
Right now, the only thing I get in the log is
~~~ Now updating all listing prices from Amazon API ~~~
I've tried adding a logging piece into the routine that is called and the routines all run successfully and log what they should, but promise.all.then doesn't execute.
I've tried just doing:
Promise.all(bleh).then(console.log("We did it"));
and that worked, but when I put a function in the Then, nothing runs.
Please help!
I'm trying to execute an asynchronous routine for a bunch of items in a list that I get from a database, but I'm having trouble understanding how promise.all works and what it does.
Here is the code I'm using right now:
/**
* Queues up price updates
*/
function updatePrices() {
console.log("~~~ Now updating all listing prices from Amazon API ~~~");
//Grabs the listings from the database, this part works fine
fetchListings().then(function(listings) {
//Creates an array of promises from my listing helper class
Promise.all(listings.map(function(listing){
//The promise that resolves to a response from the routine
return(listing_helper.listingPriceUpdateRoutine(listing.asin));
})).then(function(results){
//We want to log the result of all the routine responses
results.map(function(result){
console.log(result);
});
//Let us know everything finished
console.log("~~~ Listings updated ~~~");
}).catch(function(err){
console.log("Catch: ", err);
});
});
}
Right now, the only thing I get in the log is
~~~ Now updating all listing prices from Amazon API ~~~
I've tried adding a logging piece into the routine that is called and the routines all run successfully and log what they should, but promise.all.then doesn't execute.
I've tried just doing:
Promise.all(bleh).then(console.log("We did it"));
and that worked, but when I put a function in the Then, nothing runs.
Please help!
Share Improve this question asked Apr 30, 2016 at 17:10 user2172205user2172205 2551 gold badge4 silver badges10 bronze badges 8- tl;dr; - you're missing a return before a promise in a chain. – Benjamin Gruenbaum Commented Apr 30, 2016 at 17:12
-
Ok, had a quick look at the code - you're missing two
return
s. One before yourPromise.all
and one beforefetchListings()
, promises work by return value - if you don't return from athen
handler it doesn't wait for anything. – Benjamin Gruenbaum Commented Apr 30, 2016 at 17:13 - Also, are all the functions you're calling promise-returning functions? You need to isolate your problem further. – Benjamin Gruenbaum Commented Apr 30, 2016 at 17:13
- did you console.log all of your isting_helper.listingPriceUpdateRoutine(listing.asin)? did really all of the finish (count === listens.length?) – lipp Commented Apr 30, 2016 at 17:21
- @BenjaminGruenbaum your suggestion didn't seem to change my results. The updatePrices doesn't need to return anything, it just needs to log information. I tried putting the return in front of the Promise.all and in front of the fetchListings().then but it changed nothing. – user2172205 Commented Apr 30, 2016 at 17:30
1 Answer
Reset to default 7Promise.all()
itself is pretty simple. You pass it an array of promises. It returns a new promise that will resolve when all the promises in your array resolve or will reject when any individual promise in the array rejects.
var pAll = Promise.all([p1, p2, p3]);
pAll.then(function(r) {
// all promises resolved
// r is an array of results
}, function(err) {
// one or more promises rejected
// err is the reason for the first promise that rejected
});
Some reasons that Promise.all()
might not work in your code:
- You aren't passing an array of promises to it.
- Some of the promises in the array you pass never resolve or reject thus
Promise.all()
never resolves/rejects its master promise - You aren't properly passing callbacks to
.then()
handlers where you should be - You aren't properly returning promises from internal functions so they propagate out or chain properly
Your example:
Promise.all(bleh).then(console.log("We did it"));
Is wrong. You must pass a function reference to .then()
like this:
Promise.all(bleh).then(function() {
console.log("We did it")
});
In your case, the console.log()
would execute immediately and not wait for the promises to be resolved.
In your detailed code are you 100% sure that:
listing_helper.listingPriceUpdateRoutine(listing.asin)
is returning a promise? And, that that promise will get resolved or rejected properly?
Note to Readers - If you read all the ments, you can see that the OP's actual issue was not with Promise.all()
, but they were getting rate limited for sending requests too quickly to the target host. Since that should have been propagating a request error back which should have been easily visible, the OP apparently also has a problem with error handling or propagation which is likely in code that was not disclosed here.
本文标签: javascriptNode JS Async PromiseAll issuesStack Overflow
版权声明:本文标题:javascript - Node JS Async Promise.All issues - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745200027a2647326.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论