await-cycle
v1.0.6
Published
It accepts an array for enumeration and an async function, the result of which returns a promise. Calls a function on each element of the array, further code execution will wait until all functions return resolve
Maintainers
Readme
The function within which the cycle is executed is called in the asynchronous block. Accepts an async function that should return a promise. Will be resolve when all transferred functions are resolved. The transferred functions are called parallel!
Install
$ yarn add await-cycle
or
$ npm i await-cycle
Example Usage
const awaitCycle = require("await-cycle");
const arr = ["a", "b", "c", "d"];
const delay = (item, idx) => {
return new Promise(resolve =>
setTimeout(() => {
console.log(`in item ${item} - ${idx}`);
resolve();
}, 500)
);
};
(async () => {
await awaitCycle(arr, (item, idx) => delay(item, idx));
console.log("after!");
})();Result
in item a - 0
in item b - 1
in item c - 2
in item d - 3
after!