Skip to content Skip to sidebar Skip to footer

Promise.all() - How To Resolve() Without Returning Undefined Or Value

I want to use Promise.all() to check if a value is in an array. My problem is when the value is not found in the array, the promise returns undefined, but I would like to only have

Solution 1:

This doesn't seem to be possible. I would file it away as a "sane default", because it is very easy to opt-in to the behavior you want, but the inverse isn't true.

E.g.:

Promise.all(foundValues)
  .then(function(values) {
     return values.filter(function(value) { return typeof value !== 'undefined';});
  })
  .then(function(values) {
    console.log(values) // [1, 5, 10]
  });

Solution 2:

I think it's not possible to make Promise.all to do that. There is no feature like that in JavaScript Promise. A Promise cannot resolve or reject without a value.

Can this code answer your question: values.filter(value => value !== undefined);(Chrome, Opera, Safari, Firefox(in-use version), and IE 9+ support Array.prototype.filter.)?


Post a Comment for "Promise.all() - How To Resolve() Without Returning Undefined Or Value"