async和await知识看起来像同步代码,实际上是异步代码
<script src="http://libs.baidu.com/jquery/1.9.0/jquery.js"></script>
<script>
function pAjax(options={}){
return new Promise(function (resolve,reject){
$.ajax({
url:options.url,
data:options.data,
async:options.async,
dataType:options.dataType,
type:options.type,
success(res){
resolve(res)
},
error(err){
reject(err)
}
})
})
}
async function a(){
const one= await pAjax({url:"https://api-hmugo-web.itheima.net/api/public/v1/goods/search",dataType:"json"})
console.log("第一次请求");
console.log(one);
const two= await pAjax({url:"https://api-hmugo-web.itheima.net/api/public/v1/goods/search",dataType:"json"})
console.log("第二次请求");
console.log(two);
const three= await pAjax({url:"https://api-hmugo-web.itheima.net/api/public/v1/goods/search",dataType:"json"})
console.log("第三次请求");
console.log(three);
const four= await pAjax({url:"https://api-hmugo-web.itheima.net/api/public/v1/goods/search",dataType:"json"})
console.log("第四次请求");
console.log(four);
}
a()