一个AJAX请求搭配await使用的例子:
function sendAJAX(url) {
return new Promise((resolve, reject) => {
const http_ = new XMLHttpRequest()
http_.open("get", url)
http_.send()
http_.onreadystatechange = function () {
if (http_.readyState == 4) {
if (http_.status >= 200 && http_.status < 300) {
let res = http_.response
resolve(res)
} else {
reject(http_.status)
}
}
}
})
}
sendAJAX("https://pro.jd.com/mall/active/").then(value => {
console.log(value)
})
async function async_sendAAJX_() {
try {
let data = await sendAJAX("https://pro.jd.com/mall/active/")
console.log(data)
} catch (error) {
}
}
async_sendAAJX_()