简体   繁体   中英

Returning an array of promises for Promise.all using filter not working

I have an array of objects and for each I execute a promise. Then, if all resolve, I expect to get a .then call in Promise.all . Problem is that with the following example I only get the output

All done in approach 2

I think the following code is pretty much explanatory but, in a nutshell, what I'm doing is:

  1. Declare 3 objects as vals and I want to create a promise based on each.
  2. Approach 1: use vals.map to get an array of promises based on vals
  3. Approach 2: iterate through vals using a for and push a new promise in each iteration.

What's wrong with this code and why is it working only for approach 2?

#!/usr/bin/env node

let vals = [
    {
        name: "name1"
    },
    {
        name: "name2"
    },
    {
        name: "name3"
    }
];

function newPromise(obj) {
    return new Promise((resolve) => Promise.resolve(obj.name))
}

/**
 * Approach 1: use .map function to get an array of promises
 */

let promises = vals.map((item) => newPromise(item));
Promise.all(promises)
    .then(() => console.log("All done in approach 1"))
    .catch(() => console.log("Error in approach 1"));

/**
 * Approach 2: iterate through vals using a for an push a new promise for the promises array
 */

let promises2 = [];
for (let i = 0 ; i < vals.length; i++) {
    promises.push(newPromise(vals[i]));
}
Promise.all(promises2)
    .then(() => console.log("All done in approach 2"))
    .catch(() => console.log("Error in approach 2"));

I'm using node 8.9.1 with ES6 (native Promises).

You have two slight errors which are gumming up the works and confusing things.

First off, your second approach shouldn't work because you accidentally had it pushing them to promises for the second approach instead of promises2 , so you're doing Promise.all() with an empty array, which is why it worked.

Then, the reason both don't work because of how you're creating the promises. You were nesting Promise.resolve() inside new Promise() , which doesn't work. You could use either:

return new Promise(resolve => resolve(obj.name));

or

return Promise.resolve(obj.name);

Those are equivalent. Having them inside, you never called the proper resolve() function in new Promise() , so nothing ever actually resolved.

 let vals = [ { name: "name1" }, { name: "name2" }, { name: "name3" } ]; function newPromise(obj) { return new Promise((resolve) => resolve(obj.name)) } /** * Approach 1: use .map function to get an array of promises */ let promises = vals.map((item) => newPromise(item)); Promise.all(promises) .then(() => console.log("All done in approach 1")) .catch(() => console.log("Error in approach 1")); /** * Approach 2: iterate through vals using a for an push a new promise for the promises array */ let promises2 = []; for (let i = 0 ; i < vals.length; i++) { promises2.push(newPromise(vals[i])); } Promise.all(promises2) .then(() => console.log("All done in approach 2")) .catch(() => console.log("Error in approach 2")); 

You have a problem with your newPromise function. Try to remove Promise. .

For the second approach, you also have to push on the right array : promises2

 let vals = [ { name: "name1" }, { name: "name2" }, { name: "name3" } ]; function newPromise(obj) { return new Promise((resolve) => resolve(obj.name)) } /** * Approach 1: use .map function to get an array of promises */ let promises = vals.map((item) => newPromise(item)); Promise.all(promises) .then(() => console.log("All done in approach 1")) .catch(() => console.log("Error in approach 1")); /** * Approach 2: iterate through vals using a for an push a new promise for the promises array */ let promises2 = []; for (let i = 0 ; i < vals.length; i++) { promises2.push(newPromise(vals[i])); } Promise.all(promises2) .then(() => console.log("All done in approach 2")) .catch(() => console.log("Error in approach 2")); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM