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

javascript - How do I reduce multiple Promise.all? - Stack Overflow

programmeradmin1浏览0评论

I am trying to use a Promise.all inside of a reduce and cannot get my function to work, unless there is only one user in my array. The starting object of the reduce is a Promise. The first time through the reduce, the Promise has .all available on it. The second time through, the .all is not available.

return UserQueries.addUsersOnCasefileCreate(input).then(users => {
  return users.reduce((promise, user) => {
    return promise.all([
      AddressQueries.addAddress(user.address, user.userId, inputId),
      EmailQueries.addEmail(user.emails, user.userId, inputId),
      PhoneQueries.addPhones(user.phones, user.userId, inputId)
    ])
    .then(() => Promise.resolve(user))
  }, Promise);
})

How could I perform this operation?

I am trying to use a Promise.all inside of a reduce and cannot get my function to work, unless there is only one user in my array. The starting object of the reduce is a Promise. The first time through the reduce, the Promise has .all available on it. The second time through, the .all is not available.

return UserQueries.addUsersOnCasefileCreate(input).then(users => {
  return users.reduce((promise, user) => {
    return promise.all([
      AddressQueries.addAddress(user.address, user.userId, inputId),
      EmailQueries.addEmail(user.emails, user.userId, inputId),
      PhoneQueries.addPhones(user.phones, user.userId, inputId)
    ])
    .then(() => Promise.resolve(user))
  }, Promise);
})

How could I perform this operation?

Share Improve this question asked Apr 16, 2017 at 4:16 jhammjhamm 25.1k42 gold badges110 silver badges188 bronze badges 7
  • There's an obvious case typo between promise and Promise, anyway. – Tatsuyuki Ishi Commented Apr 16, 2017 at 4:31
  • The promise that is lower case is just the first argument of the reduce referring to the Promise as the initial object. It isn't a typo. – jhamm Commented Apr 16, 2017 at 4:36
  • There is no Promise.prototype.all(). Only Promise.all(). – Tatsuyuki Ishi Commented Apr 16, 2017 at 4:38
  • The promise from the first argument is the Promise in the initial object. That is why it works the first time through the loop. That is why Promise.all is available at first. – jhamm Commented Apr 16, 2017 at 4:39
  • 1 @jhamm - no, you don't understand - there is no Promise.prototype.all - your code as written will fail with errors – Jaromanda X Commented Apr 16, 2017 at 5:43
 |  Show 2 more ments

2 Answers 2

Reset to default 3

You initialize with Promise which is a function, though return a resolved Promise object, where the two are not the same.

You can initialize with Promise.resolve(), call promise.then(), then return Promise.all() with .then() chained within first .then(), which passes Promise object to next iteration at .reduce().

return UserQueries.addUsersOnCasefileCreate(input).then(users => {
  return users.reduce((promise, user) => {
    return promise.then(() => Promise.all([
      AddressQueries.addAddress(user.address, user.userId, inputId),
      EmailQueries.addEmail(user.emails, user.userId, inputId),
      PhoneQueries.addPhones(user.phones, user.userId, inputId)
    ]))
    .then(() => user))
  }, Promise.resolve());
})

There's no need to use reduce(). Just map the things and wait them all.

return UserQueries.addUsersOnCasefileCreate(input).then(users => {
  return Promise.all(users.map((user) => {
    return Promise.all([
      AddressQueries.addAddress(user.address, user.userId, inputId),
      EmailQueries.addEmail(user.emails, user.userId, inputId),
      PhoneQueries.addPhones(user.phones, user.userId, inputId)
    ]);
  }));
});
发布评论

评论列表(0)

  1. 暂无评论