vue中对api的管理,对异步最好处理

/*
async:异步 await 等待 等待上一个promise执行完毕以后才会执行下一个pormise 同时await还接收resolve中的参数

语法 var fn = asnyc function(){}

async最好配合promise一起使用
*/

//var fn = async function(){} //当前函数是异步


var fn1 = function(){
return new Promise((resolve)=>{
setTimeout(()=>{
console.log(111);
var obj = {name:1}
resolve(obj);
},2000)
})
}

var fn2 = function(){
return new Promise((resolve)=>{
setTimeout(()=>{
console.log(222)
resolve();
},1000)
})
}

var fn3 = function(){
return new Promise((resolve)=>{
setTimeout(()=>{
console.log(333)
resolve();
},500)
})
}


// var p = new Promise(resolve=>{
// setTimeout(()=>{
// console.log(0000)
// resolve();
// },3000)
// })

// p.then(()=>{
// return fn1();
// })
// .then(()=>{
// return fn2();
// })
// .then(()=>{
// fn3();
// })


// var test = async ()=>{
// await fn1();
// await fn2();
// await fn3();
// }
// test();


var test = async ()=>{
let data = await fn1();
console.log(data);
}
test();

原文地址:https://www.cnblogs.com/PeiGaGa/p/11032692.html