基本的生成器
function * foo(){console.log("start")
}
const generator = foo()
generator.next()
生成器的返回值
function* foo() {console.log("start")yield "mara"
}
const generator = foo()
const result = generator.next()
console.log("result=", result)
生成器的参数
function* foo() {console.log("start")const arg = yield "mara"console.log("arg = ", arg)
}
const generator = foo()
const result = generator.next()
console.log("result=", result)
generator.next("hello")
生成器结合 ajax 使用
function ajax(url) {return new Promise(function (resolve, reject) {const xhr = new XMLHttpRequest()xhr.open("GET", url)xhr.responseType = "json"xhr.onload = function () {if (this.status === 200) {resolve(this.response)} else {reject(new Error(this.statusText))}}xhr.send()})
}
function* userRequest() {const users = yield ajax("/users.json")console.log(users)
}
const generator = userRequest()
const result = generator.next()
result.value.then(data => generator.next(data))
多个 ajax 请求
function ajax(url) {return new Promise(function (resolve, reject) {const xhr = new XMLHttpRequest()xhr.open("GET", url)xhr.responseType = "json"xhr.onload = function () {if (this.status === 200) {resolve(this.response)} else {reject(new Error(this.statusText))}}xhr.send()})
}
function* userRequest() {const users = yield ajax("/users.json")console.log(users)const urls = yield ajax("/urls.json")console.log(urls)
}
const generator = userRequest()
let result = generator.next()
result.value.then(data => {if (data.done) returnresult = generator.next(data)if (result.done) return;result.value.then(data=>{if (data.done) returngenerator.next()})
})
错误处理
function ajax(url) {return new Promise(function (resolve, reject) {const xhr = new XMLHttpRequest()xhr.open("GET", url)xhr.responseType = "json"xhr.onload = function () {if (this.status === 200) {resolve(this.response)} else {reject(new Error(this.statusText))}}xhr.send()})
}
function* userRequest() {const users = yield ajax("/users.json")console.log(users)const urls = yield ajax("/urls.json")console.log(urls)try{const mockError = yield ajax("/urls333.json")console.log("mockError", mockError)}catch (e){console.log("xxx", e)}
}
const generator = userRequest()
function handleResult(result) {if (result.done) returnresult.value.then(data => {handleResult(generator.next(data))}).catch(err => generator.throw(err))
}
handleResult(generator.next())
封装通用的执行器方法
function ajax(url) {return new Promise(function (resolve, reject) {const xhr = new XMLHttpRequest()xhr.open("GET", url)xhr.responseType = "json"xhr.onload = function () {if (this.status === 200) {resolve(this.response)} else {reject(new Error(this.statusText))}}xhr.send()})
}
function* userRequest() {const users = yield ajax("/users.json")console.log(users)const urls = yield ajax("/urls.json")console.log(urls)try{const mockError = yield ajax("/urls333.json")console.log("mockError", mockError)}catch (e){console.log("xxx", e)}
}
function co(generator){const g = generator()function handleResult(result) {if (result.done) returnresult.value.then(data => {handleResult(g.next(data))}).catch(err => g.throw(err))}handleResult(g.next())
}
co(userRequest)
使用 async 和 await 语法糖
function ajax(url) {return new Promise(function (resolve, reject) {const xhr = new XMLHttpRequest()xhr.open("GET", url)xhr.responseType = "json"xhr.onload = function () {if (this.status === 200) {resolve(this.response)} else {reject(new Error(this.statusText))}}xhr.send()})
}
async function userRequest() {const users = await ajax("/users.json")console.log(users)const urls = await ajax("/urls.json")console.log(urls)try {const mockError = await ajax("/urls333.json")console.log("mockError", mockError)} catch (e) {console.log("xxx", e)}
}userRequest().then(resp => console.log(resp)).catch(error => console.log(error))