Created
June 14, 2018 08:52
-
-
Save ferreiratiago/590e46363f5251cbd653548a9bdcf887 to your computer and use it in GitHub Desktop.
Async Iterator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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