A TypeScript library of asynchronous array functions with Promise compatibility.
Explore the docs »
Report Bug
·
Request Feature
Table of Contents
Native JavaScript array loops such as Array.prototype.map() are incompatible with Promises. Writing asynchronous within a map, even if wrapped in Promise.all will result in inconsistent behavior.
For example, if we want to fetch three dad jokes by ID from the icanhazdadjoke web API then we unfortunately cannot maintain the order of the IDs in our response. Using map from async-iterators will maintain order.
await Promise.all(
["0ga2EdN7prc", "4EBsPZvP7h", "xHQucUvszd"].map(async (jokeId, index) => {
await fetch(`https://icanhazdadjoke.com/j/${jokeId}`, {
headers: { Accept: "application/json" },
})
.then((response) => response.json())
.then((json) => console.log(`Joke #${index}: ${json.joke}`));
})
);
// Order of the logs is not guaranteed.
// Joke #1: Some people eat light bulbs. They say it's a nice light snack.
// Joke #2: To the guy who invented zero... thanks for nothing.
// Joke #0: How come the stadium got hot after the game? Because all of the fans left.import { map } from "async-iterators";
await map(
["0ga2EdN7prc", "4EBsPZvP7h", "xHQucUvszd"],
async (jokeId, index) => {
await fetch(`https://icanhazdadjoke.com/j/${jokeId}`, {
headers: { Accept: "application/json" },
})
.then((response) => response.json())
.then((json) => console.log(`Joke #${index}: ${json.joke}`));
}
);
// Order is guaranteed.
// Joke #0: How come the stadium got hot after the game? Because all of the fans left.
// Joke #1: Some people eat light bulbs. They say it's a nice light snack.
// Joke #2: To the guy who invented zero... thanks for nothing.This package has no dependencies and never will. Simply install the NPM package and you're ready to use it!
git clone https://github.com/GuthrieW/async-iterators.gitnpm install async-iteraorsyarn add async-iteratorsFor more examples, please refer to the documentation
See the open issues for a full list of proposed features (and known issues).
Please consider contributing to the project if you open an issue!
Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.
If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement". Don't forget to give the project a star! Thanks again!
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature) - Commit your Changes (
git commit -m 'Add some AmazingFeature') - Push to the Branch (
git push origin feature/AmazingFeature) - Open a Pull Request
Distributed under the MIT License. See LICENSE.txt for more information.
Wesley Guthrie - @twitter_handle - wesleyeguthrie@gmail.com
Project Link: https://github.com/GuthrieW/async-iterators
- Gregory Spicer and Joshua Sarna for both introducing me to this issue and working on solutions together