- async/await是ES7引入的新语法,可以更加方便的进行异步操作
- async关键字用于函数上(async函数的返回值是Promise实例对象)
- await关键字用于async函数当中(await可以得到异步的结果)
async function queryData(id){
const ret = await axios.get('/url');
return ret;
}
queryData.then((ret)=>{
console.log(ret);
});
async/await处理多个异步请求
async function queryData(id){
const info= await axios.get('url1')
const ret = await axios.get('/url?info='+info.data);
return ret;
}
queryData.then((ret)=>{
console.log(ret);
});
当info请求完成后才请求ret