Skip to content

Instantly share code, notes, and snippets.

@ferreiratiago
Created June 14, 2018 08:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ferreiratiago/590e46363f5251cbd653548a9bdcf887 to your computer and use it in GitHub Desktop.
Save ferreiratiago/590e46363f5251cbd653548a9bdcf887 to your computer and use it in GitHub Desktop.
Async Iterator
function asyncIterator() {
const array = [1, 2];
return {
next: function() {
if (array.length) {
return Promise.resolve({
value: array.shift(),
done: false
});
} else {
return Promise.resolve({
done: true
});
}
}
};
}
var iterator = asyncIterator();
(async function() {
await iterator.next().then(console.log); // { value: 1, done: false }
await iterator.next().then(console.log); // { value: 2, done: false }
await iterator.next().then(console.log); // { done: true }
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment