简体   繁体   中英

Perform async operations with Promise.all issue

I am new to nodejs and for me Promise seems to be a bit difficult to understand. I have a requirement where I have an array of objects (assume large data from database). I need to go through each data and perform two async operations(database operation) parallely for that particular data.

Here is the sample snippet attached.

const data = [
 {
     "id": 1
 },
 {
     "id": 2
 },
 {
     "id": 3
 },
 {
     "id": 4
 },
 {
     "id": 5
 }
];


(async () =>  {
    
    const first = async(id) => {
      // db operation  
      return new Promise(resolve => setTimeout(() => {
        console.log("first", id);
        resolve();
      }, 100 * Math.random()));
    };
    
    const second = async(id) => {
        // db operation
        return new Promise(resolve => setTimeout(() => {
        console.log("second", id);
        resolve();
      }, 100 * Math.random()));
    };
    
    const promiseResp = data.map(async(d) => {
        await first(d.id);
        await second(d.id);
    });
    
    await Promise.all(promiseResp);
})();

Response Getting:

first 5
first 3
second 5
first 4
first 2
first 1
second 3
second 2
second 4
second 1

I am expecting the code be run parallel for the same data. But the response is not giving the way I am expecting. My expectation is for the same id, perform both first & second async operations parallely and provide output before moving to next object. How can we achieve this?

Can somebody let me know where I am missing? Any help would be really appreciated?

Edit : Also how can we perform parallel operations of all the data?

To run first and second in parallel, you wouldn't use await but rather

const promises = data.map(d =>
    Promise.all([
        first(d.id),
        second(d.id),
    ])
);
await Promise.all(promises);

provide output before moving to next object

For that, you must not use data.map but a sequential loop that await s the result per object:

for (const d of data) {
    await Promise.all([
        first(d.id),
        second(d.id),
    ]);
}

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