最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - map() with async vs promise.all() - Stack Overflow

programmeradmin4浏览0评论

If I have an array of elements and I want to do parallel operations on them.

I would use promise.all().

I knew promise.all() accepts array of promises. Correct me if I am wrong, I don't think so.

Here, it clearly says.

The Promise.all() method returns a single Promise that fulfills when all of the promises passed as an iterable have been fulfilled or when the iterable contains no promises or when the iterable contains promises that have been fulfilled and non-promises that have been returned. It rejects with the reason of the first promise that rejects, or with the error caught by the first argument if that argument has caught an error inside it using try/catch/throw blocks.

So, yes we can pass simple functions to promise.all(), and it resolves if they return and rejects if they throw an error.

Now look at the below code.

const promises = todayAssignedJobs.map(async todayAssigned => {
  const [leaderboard, created] = await Leaderboard.findOrCreate({...});

  if (!created) {
    const rating = (todayAssigned.rating + leaderboard.rating * leaderboard.jobs_pleted) / (leaderboard.jobs_pleted + 1);
    const mission = todayAssignedmission + leaderboardmission;
    const jobsCompleted = leaderboard.jobs_pleted + 1;

    await Leaderboard.update({
      rating,
      mission,
      jobs_pleted: jobsCompleted,
      updated_by: 'system',
    }, {
      where: {
        id: leaderboard.id,
      },
    });
  }

  await AssignedJob.update({
    is_leaderboard_generated: true,
  }, {
    where: {
      id: todayAssigned.id,
    },
  });
});

await Promise.all(promises);

Here, I have a doubt.

We are iterating on each element of the array and doing operation on them asyncrhonously. They don't return anything explicitly.

So, I think map is doing parallel operations here too.

Why shuold then use promise.all() here?

If I have an array of elements and I want to do parallel operations on them.

I would use promise.all().

I knew promise.all() accepts array of promises. Correct me if I am wrong, I don't think so.

Here, it clearly says.

The Promise.all() method returns a single Promise that fulfills when all of the promises passed as an iterable have been fulfilled or when the iterable contains no promises or when the iterable contains promises that have been fulfilled and non-promises that have been returned. It rejects with the reason of the first promise that rejects, or with the error caught by the first argument if that argument has caught an error inside it using try/catch/throw blocks.

So, yes we can pass simple functions to promise.all(), and it resolves if they return and rejects if they throw an error.

Now look at the below code.

const promises = todayAssignedJobs.map(async todayAssigned => {
  const [leaderboard, created] = await Leaderboard.findOrCreate({...});

  if (!created) {
    const rating = (todayAssigned.rating + leaderboard.rating * leaderboard.jobs_pleted) / (leaderboard.jobs_pleted + 1);
    const mission = todayAssigned.mission + leaderboard.mission;
    const jobsCompleted = leaderboard.jobs_pleted + 1;

    await Leaderboard.update({
      rating,
      mission,
      jobs_pleted: jobsCompleted,
      updated_by: 'system',
    }, {
      where: {
        id: leaderboard.id,
      },
    });
  }

  await AssignedJob.update({
    is_leaderboard_generated: true,
  }, {
    where: {
      id: todayAssigned.id,
    },
  });
});

await Promise.all(promises);

Here, I have a doubt.

We are iterating on each element of the array and doing operation on them asyncrhonously. They don't return anything explicitly.

So, I think map is doing parallel operations here too.

Why shuold then use promise.all() here?

Share Improve this question edited Apr 23, 2020 at 5:10 yossi 3,1647 gold badges48 silver badges70 bronze badges asked Apr 23, 2020 at 5:06 SujeetSujeet 3,8105 gold badges35 silver badges67 bronze badges 14
  • 1 Why shuold then use promise.all() here? As opposed to..? Were you thinking of a for loop instead? – CertainPerformance Commented Apr 23, 2020 at 5:08
  • 1 "They don't return anything explicitly"
发布评论

评论列表(0)

  1. 暂无评论