ES7 async Array methods with arraync

Barna Toth
Frontend journeys
Published in
1 min readNov 25, 2016

I’ve always loved the concept of async/await in JavaScript and been using it since BabelJS started to support it. Since then I switched to TypeScript ❤ which support async/await for ES5 since version 2.1.

If you start using async/await it immediately becomes obvious that the built in Array methods simply doesn’t support usual use cases with async/await.

For example in the code below I’ve tried to map an array to a new one and get the new array item values asynchronously. Clearly failed to do so as the Array prototype’s map method doesn’t wait for my async function to return.

As you can see myItems simply became an array of Promises as async functions return Promises — not what I wanted :)

After some research I’ve found no library that solves this issue by adding an Array polyfill and supporting TypeScript, so I wrote one:

Finally with the “arraync” library I can simply do this:

The array I got contains the items returned from the server while keeping the code simple and readable.

Hope “arraync” will be helpful to all others out there with this problem.

Thanks for reading.

--

--