scync/await 说明
async function f() {
return "hello async"
}
const fn = f();
console.log(fn);
fn.then(res=>{
console.log(res)
})
console.log('虽然在后面,但是我先执行');
await
function doubleA(num) {
let flang = true
return new Promise((resolve, reject) => {
setTimeout(() => {
if(flang){
resolve(2 * num)
}else {
reject('出错了~~')
}
}, 2000);
} )
}
async function testResult() {
let first,second,third
try {
first = await doubleA(30);
second = await doubleA(50);
third = await doubleA (30);
}catch (e) {
console.log(e)
}
console.log(first + second + third);
}
testResult();
转载请注明原文地址:https://ipadbbs.8miu.com/read-41079.html