p-iteration-with-concurrency
Make array iteration easy when using async/await and promises
- Same functionality as the ES5 Array iteration methods we all know
- All the methods return a
Promise
, making them awaitable and thenable - Allow the usage of async functions as callback
- Callbacks run concurrently
- Lightweight (no prd dependencies)
Install
$ npm install --save p-iteration-with-concurrency
Usage
Smooth asynchronous iteration using async/await
:
const map = ; // map passing an async function as callback { return ;} // map passing a non-async function as callback { const responses = await ; // ... do some stuff return responses;} // ...
const filter = ; { const filteredUsers = await ; // ... do some stuff return filteredUsers;} // ...
All methods return a Promise so they can just be used outside an async function just with plain Promises:
const map = ; ;
If there is a Promise in the array, it will be unwrapped before calling the callback:
const forEach = ;const fetchJSON = ; { const users = // returns a Promise userId: 123 name: 'Jolyne' age: 19 userId: 156 name: 'Caesar' age: 20 ; return ;}
const find = ;const fetchJSON = ; { const users = // returns a Promise userId: 123 name: 'Jolyne' age: 19 userId: 156 name: 'Caesar' age: 20 ; return ;}
The callback will be invoked as soon as the Promise is unwrapped:
const forEach = ; // function that returns a Promise resolved after 'ms' passedconst delay = ; // 100, 200, 300 and 500 will be logged in this order { const numbers = 100 ; await ;}