1.await必须在async定义的方法中使用
let e = await 78910;
console.log(e);
2.当await右边为一个PromiseState为fulfilled的Promise对象时,则返回其PromiseResult值
async function main() {
let p = new Promise((resolve, reject) => {
resolve(156);
});
let result = await p;
console.log(result);
let p1 = new Promise((resolve, reject) => {
reject(177);
});
try {
let result1 = await p1;
} catch (e) {
console.log(e);
}
}
main();
3.当await右边为为一个PromiseResult为rejected的Promise对象时,需要trycatch获取其PromiseResult
async function main() {
let p = new Promise((resolve, reject) => {
resolve(156);
});
let result = await p;
console.log(result);
let p1 = new Promise((resolve, reject) => {
reject(177);
});
try {
let result1 = await p1;
} catch (e) {
console.log(e);
}
}
main();
4.如果await右边为一个非Promise对象,则直接返回右边内容
async function main() {
let e = await 78910;
console.log(e);
}
main();
作者:少帅