yourfavorite yourfavorite - 1 month ago 6
Javascript Question

Nested promises and how to get around em

I am using a promise to get some JSON from a URL. The JSON that is returned includes a list of new URLs that return JSON. My current implementation is failing due to the nested promises.

I need to do the following:


  1. request parent JSON url

  2. request each of the child JSON urls

  3. After each child promise returns JSON, I need to do some stuff with the child's JSON and the parent JSON.



I am getting the following error.

Warning: a promise was created in a handler at main.development.js:661:61 but was not returned from it


Boiled down version of my code:

myPromise(url)
.then(response => {
// process the data into an array of items
items.forEach(item => {
myPromise(item.url)
.then(response2 => {
// Do a thing here with data from response and response2
});
});
});

Answer

Here I've done your example, using Bluebird map.

I've also added the concurrency option, this is very handy.. Leaving out, will just work a bit like promise.all, and putting a value of 1, would be were you want to do all the promises in series..

myPromise(url)
  .then(response => {
    // process the data into an array of items
   return Promise.map(items, item => {
     return myPromise(item.url)
        .then(response2 => {
          // Do a thing here with data from response and response2
        });
    }, {concurrency:10});  //lets do a max of 10 promises at a time.
  });