ES6 Promises.all() does not execute all (no error caught in promises)

Viewed 158

I am trying to create an array of Promises which would print a message to the console when fulfilled. However, it seems to only print once even though there are 10 promise objects within the array.

I have tried the solution in ES6 Promises/calling a function after multiple promises are fulfilled (can't use Promises.all) but it still does not work.

function fillArrayWithPromise(promise, numTimes) {
  let promiseArr = Array(numTimes).fill(promise);
  console.log(promiseArr.join("-"));
  return promiseArr
}

var sendStuffB = new Promise(
  function(resolve, reject) {
    try {
      console.log("submitted one req");
    } catch (error) {
      console.log("Error: " + error);
    }
  }
)

let testA = fillArrayWithPromise(sendStuffB, 10);

Promise.all(
  testA.map(promise => Promise.resolve(promise).catch(err => console.log(error)))
).then(function() {
  console.log("End");
});

I expected the console to print 10 times of "One object sent" but it is only printed to the console once and continues to run indefinitely afterwards.

Here is the error log: Running Test Script... One object sent submitted one req [object Promise]-[object Promise]-[object Promise]-[object Promise]-[object Promise]-[object Promise]-[object Promise]-[object Promise]-[object Promise]-[object Promise]

3 Answers

Because sendStuffB is only one promise, so it is resolved only once. All the other items in your array are reference to the same resolved promise.

Instead of this, in fillArrayWithPromise function, pass a function which returns a promise, and call that function seperately for each array item.

Also, always remember to resolve or reject the promise in the promise constructor function as needed.

function fillArrayWithPromise(promiseFn, numTimes) {
  let promiseArr = Array(numTimes).fill(promiseFn).map(fn => fn()); //see this
  return promiseArr
}

var sendStuffB = () => new Promise(  // converted to function
  function(resolve, reject) {
    try {
      console.log("submitted one req");
      resolve(); // remember to resolve
    } catch (error) {
      console.log("Error: " + error);
      reject(error); // reject
    }
  }
)

let testA = fillArrayWithPromise(sendStuffB, 10);

Promise.all(
  testA.map(promise => Promise.resolve(promise).catch(err => console.log(error)))
).then(function() {
  console.log("End");
});

You are not calling the resolve function in the promises, so they never complete. Try doing this:

var sendStuffB = new Promise(
  function(resolve, reject) {
    try {
      console.log("submitted one req");
      resolve();
    } catch (error) {
      console.log("Error: " + error);
      reject(error);
    }
  }
)

This is a basic example of the Promise.all functionality.

It might help you.

const delayArray = [1000,1500,2000];

const promises = delayArray.map((item) => {
  return new Promise((resolve,reject) => {
    setTimeout(()=>{
      console.log('I was resolved in' + item + 'ms');
      resolve('Resolved' + item + 'ms');
    },item);
  });
});

Promise.all(promises)
.then((values) => console.log(values));

Related