Created
September 27, 2022 12:17
-
-
Save rayepeng/e1b3eab0918dd72698cbc26e0713bd53 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
| function *fetchHttpBin(){ | |
| const data = yield fetch('https://httpbin.org/get'); // 等待返回值 | |
| console.log(data); | |
| } | |
| function main2(){ | |
| const g = fetchHttpBin(); // 获得生成器 | |
| g.next() | |
| .value // 执行到next,此时生成器返回 {done, value} | |
| .then( (data)=> data.json()) // 执行到yield,此时会返回promise | |
| .then( (data)=>g.next(data)); // 返回数据传递给yield | |
| } | |
| main2() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment