Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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