Skip to content

Instantly share code, notes, and snippets.

@wjx0912
Last active December 28, 2021 12:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wjx0912/ffa0a784702fc0b59391cd15c9868f77 to your computer and use it in GitHub Desktop.
Save wjx0912/ffa0a784702fc0b59391cd15c9868f77 to your computer and use it in GitHub Desktop.
js_generator_question
function ajax(url, callback) {
console.log('ajax enter: ', url)
// 1、创建XMLHttpRequest对象
var xmlhttp
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest()
} else { // 兼容早期浏览器
xmlhttp = new ActiveXObject('Microsoft.XMLHTTP')
}
// 2、发送请求
xmlhttp.open('GET', url, true)
xmlhttp.send()
// 3、服务端响应
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState === 4 && xmlhttp.status === 200) {
var obj = JSON.parse(xmlhttp.responseText)
// console.log(obj)
callback(obj)
}
}
console.log('ajax leave: ', url)
}
function request(url) {
console.log('request enter, url: ', url)
ajax(url, res => {
console.log('callback enter, url: ', url)
getData.next(res)
console.log('callback leave, url: ', url)
})
console.log('request leave, url: ', url)
}
function* gen() {
let res1 = yield request('http://www.httpbin.org/get?q=test1')
console.log('res1: ', res1)
let res2 = yield request('http://www.httpbin.org/get?q=test2')
console.log('res2: ', res2)
let res3 = yield request('http://www.httpbin.org/get?q=test3')
console.log('res3: ', res3)
}
let getData = gen()
getData.next()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment