Skip to content

Instantly share code, notes, and snippets.

@JustinSDK
Created May 7, 2019 05:57
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 JustinSDK/e7de599674fa8ba9394da7b6e14ace63 to your computer and use it in GitHub Desktop.
Save JustinSDK/e7de599674fa8ba9394da7b6e14ace63 to your computer and use it in GitHub Desktop.
es8-async-generator.js
// ES8 非同步產生器
async function* range(start, end) {
for(let i = start; i < end; i++) {
yield i;
}
}
// 與上頭類似的效果
function* range2(start, end) {
function* range_inner() {
for(let i = start; i < end; i++) {
yield i;
}
}
let g = range_inner();
while(true) {
yield new Promise(resolve => {
resolve(g.next());
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment