Skip to content

Instantly share code, notes, and snippets.

@rayepeng
Created September 27, 2022 12:17
Show Gist options
  • Save rayepeng/e1b3eab0918dd72698cbc26e0713bd53 to your computer and use it in GitHub Desktop.
Save rayepeng/e1b3eab0918dd72698cbc26e0713bd53 to your computer and use it in GitHub Desktop.
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