I am trying to map an async function over an array, I hope to achieve such effect:
const result = orgs.map(org =>
f(org)
.then(res => return res))
// now result should be an array of res
console.log(result)
This would not work, so I tried another way:
const result = []
orgs.map(org =>
f(org)
.then(res => result.push(res)))
// now result should be an array of res
console.log(result)
Still, this won't work.
But I can print out the result by doing:
orgs.map(org =>
f(org)
.then(res => console.log(res)))
Any ideas on how this behavior happens?
Also I am using find-rss
package over an array of links
you can use Promise.all() to aggregate the results of multiple promises. Promise is very useful is javascript.
this is a simple example, as u known, your orgs map result is an promise array, so you can use Promise.all to get what u want