'Promises.all() - how to check if all return undefined

In the following code, I want to check if the promisesArray returns any data.

const promisesArray = [this.loadData1(),this.loadData2()]

Promise.all(promisesArray).then(() => {
 console.log('promises.all callback -> ', promisesArray[0])
}

The above console.log can produce the following:

Promise {<fulfilled>: undefined}
[[Prototype]]: Promise
[[PromiseState]]: "fulfilled"
[[PromiseResult]]: undefined

If I do not specify index 0, it repeats the same output for both function calls passed to Promises.all.

I am trying to access the undefined value in both the responses but not sure how to with with each key being a multi-dimensional array.

If anyone has any ideas on how to easily check if both functions result in undefined I would be very grateful.



Solution 1:[1]

Promise.all(promisesArray)
  .then((res) => res.every((r) => r === undefined))
  .then((isUndefined) => {
    // if isUndefined is true then all promises returned undefined
  });

Solution 2:[2]

Using async / await syntax:

const results = await Promise.all([this.loadData1(),this.loadData2()]);

if (results.every(r => r === undefined)) {
  console.log('No data!');
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Shahriar Shojib
Solution 2 Regan Karlewicz