"promisified" Array descendant with some sweetener
Let's start with some fancy stuff:
const AsyncArray = require('asya'),
urls = [
'https://www.nytimes.com/',
'https://www.theverge.com/',
'https://techcrunch.com/'
]
// assuming that fetch() takes an url and returns a Promise
urls.toAsync()
.map(fetch)
.then(pages => console.log("yay! we've got an AsyncArray containing fetched pages:", pages))
Get even fancier with ES7 (Node 8+):
const total = await orderIds
.toAsync()
.reduce(async (sum, id) => sum + await getOrderAmountFromDbById(id))
console.log('total amount of orders:', total)
So, it's a modified version of the native Array
that re-implements some of its built-in methods to accept async functions, but otherwise identical.
There are some subtle differences between Array
and AsyncArray
.
AsyncArray
constructor cannot be invoked without thenew
keywordAsyncArray.from()
only takes one argument (mapFn
andthisArg
are not supported)
With npm:
npm install asya