<script>
const p1 = new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest()
xhr.open('get', 'http://www.liulongbin.top:3006/api/getbooks')
xhr.send()
xhr.onreadystatechange = function () {
//
// if (xhr.readyState === 4 && xhr.status === 200) {
// resolve(xhr.responseText)
// } else {
// reject()
// }
// 预防了xhr.readyState === 3 的情景
if (xhr.readyState === 4) {
if (xhr.status === 200) {
resolve(xhr.responseText)
} else {
reject()
}
}
}
})
console.log(p1)
</script>