Created
July 28, 2016 06:26
-
-
Save ahy4/8f31c06d43540e5fdf3e757a39a700f5 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// http://gao-tec.seesaa.net/article/427643074.html | |
function Main(g){ | |
//Generatorを一つすすめる | |
var p = g.next(); | |
//yieldしきっていたら、実行を止める | |
if(p.done) return; | |
//yieldでPromiseオブジェクトが帰ってきてるので、valueを介してthenでつなげる | |
p.value.then(()=>{ | |
//resolveされたら、(最初よりも一つ分nextされた)Generatorを再帰的に渡す | |
Main(g); | |
}); | |
} | |
//Main関数にGeneratorを渡して実行開始 | |
Main(Gen()); | |
function* Gen(){ | |
//実行時即表示 | |
console.log("a"); | |
//このyieldで2秒間待機してくれる(風に見せかけることが出来る) | |
yield new Promise((resolve)=>{ | |
setTimeout(resolve,2000); | |
}); | |
//2秒後に表示 | |
console.log("b"); | |
//このyieldで1秒間待機してくれる(風に見せかけることが出来る) | |
yield new Promise((resolve)=>{ | |
setTimeout(resolve,1000); | |
}); | |
//1秒後(実行から合計3秒後)に表示 | |
console.log("c"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment