Created
May 7, 2019 05:57
-
-
Save JustinSDK/e7de599674fa8ba9394da7b6e14ace63 to your computer and use it in GitHub Desktop.
es8-async-generator.js
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
// 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