Q:

Promise.map with multiple values

I have the following code:
Promise.map(myValues, async myValue => {
  const owner = await findOwner(myValue);

  return sendMessage(owner);
})

And now I want to understand how can I accomplish the same thing if I want to have two messages sent per each value:
Promise.map(myValues, async myValue => {
  const owner = await findOwner(myValue);
  /* I want both sendMessage and senMessageSpecific(owner) to happen but map expects just one return. How do I deal with this situation? */

  sendMessage(owner);

  sendMessageSpecific(owner);
})

What is the syntax if within a map I want to be able to return more than one promise (in some cases)? Now it obviously doesn't work because I do a return so it never gets into the second promise.

A:

I assume your two message functions return promises, and you want map to await both. In that case you can wrap them in Promise.all. Like so:
Promise.map(myValues, async myValue => {
  const owner = await findOwner(myValue);
  return Promise.all([sendMessage(owner), sendMessageSpecific(owner)]);
})

Alternatively if you don't care about the return values you could just await the two functions as you are doing with findOwner. Like so:
Promise.map(myValues, async myValue => {
  const owner = await findOwner(myValue);
  await sendMessage(owner);
  await sendMessageSpecific(owner);
})

Both ways both of the functions will happen. The difference is that in the first case they will happen in parallel where in the last case they will happen one after the other.

