NOTE: This is my experiment, you can use Rx or something better than mine one XD
At the time, I need an array method act like array.forEach methods I thinking about to create the first function in this gist.
Here I describe example ways to use these functions in code. (I'm so boring to write a story :3)
Exmaple 1: When you to update a list item one by one asynchronously. you may use asyncForEach
async function magicSpell() {
const birdIds = [1,2,3,4,5,6,7,8,9,10]; // list of items
try {
await asyncForEach(birdsIds, async (birdId) => {
await axios.post(`https://shootbirds.com/onebyone/${birdId}`, { hit: true });
});
console.log('Awesome! let make dinner.');
} catch (err) {
console.log('Whoop! All other escape!');
}
}
Example 2: When you need list of item asynchronously.
async function magicSpell() {
const cities = 'Yangon Mandalay Pathein'.split(' ');
const weatherData = await asyncMap(cities, async (cityName) => {
const { data: weatherStats } = await axios.get(`https://samples.openweathermap.org/data/2.5/weather?q=${cityName}`);
return weatherStats;
})
console.log(weatherData) // [weatherStats, weatherStats, weatherStats]
}
Example 3: Or you may want to reduce for combine function
// mock async data retrever
function getPrice(vocherId) {
const amounts = {
123: 10000,
124: 20000,
125: 30000,
126: 40000,
}
return new Promise((resolve, reject) => {
return amounts[voucherId] || 0;
})
}
async function magicSpell() {
const vouchers = '123 124 125 126'.split(' ');
const totalPrice = await asyncReduce(vouchers, async (total, voucherId) => {
return total + await getPrice(voucherId)
}, 0);
console.log(totalPrice) // 1,000,000
}