Node JS Async Promise. All problems

I am trying to execute an asynchronous procedure for a bunch of items in a list that I get from a database, but I am having trouble understanding how prom.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 magazine is

~~~ Now update all listing prices from Amazon API ~~~

I tried to add part of the log to the called routine, and all routines run successfully and log what they should, but the promise .all.then does not fulfill.

I tried only to do:

Promise.all (bleh) .then (console.log ("We did this"));

and it worked, but when I put the function in Then, nothing starts.

Please, help!

+5
source share
1 answer
Himself

Promise.all() pretty simple. You are passing an array from promises. It returns a new promise that will be resolved when all promises in your array are resolved or will be rejected when any individual promise in the array is rejected.

 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 Promise.all() may not work in your code:

  • You do not pass an array of promises to it.
  • Some of the promises in the array you pass never allow or reject, so Promise.all() never solves / does not reject its promise.
  • You are not passing callbacks correctly to .then() handlers, where you should be
  • You incorrectly return promises from internal functions so that they are distributed or properly linked

Your example:

 Promise.all(bleh).then(console.log("We did it")); 

Wrong. You must pass the function reference to .then() as follows:

 Promise.all(bleh).then(function() { console.log("We did it") }); 

In your case, console.log() will execute immediately and not wait for promises permission.


In your verbose code, you are 100% sure that:

 listing_helper.listingPriceUpdateRoutine(listing.asin) 

returns a promise? And that this promise will be removed or rejected properly?


Note to readers . If you read all the comments, you will see that the actual OP problem was not with Promise.all() , but they received a speed limit for sending requests too quickly to the target host. Since this was supposed to propagate a query error that should have been easily visible, the OP also seems to have a problem with error handling or propagation, which is probably in code that has not been disclosed here.

+5
source

All Articles