最近对promise研究比较深入,逐渐也入了迷。
关于promise的作用还不太清楚的小伙伴可以先去看我上两篇文章,都有详细说明。https://blog.csdn.net/qq_42805569/article/details/110410469
写到博客里是想总结一下,一方面是对知识点的巩固,另一方面是想给promise这块有困惑的伙伴提供一些帮助
如有错误,欢迎留言,以便改正。
封装一个Ajax请求,我们先搭建原生Ajax发送请求的结构:
const xhr =new XMLHttpRequest()
xhr.open('get','http://localhost:3000/get')
xhr.send(null)
xhr.onreadystatechange=function(){
if(xhr.readyState === 4){
if(xhr.status === 200){
console.log(xhr.responseText);
}else{
console.log(xhr.responseText)
}
}
}
备注:请求地址是json-server提供的,大家可以去搜索json-server,利用npm全局安装json-server,然后利用它起一个服务,达到模拟后台数据的效果。<