0
点赞
收藏
分享

微信扫一扫

react 通过axios获取数据 出现promise为pending——使用await获取Promise对象的PromiseResult

1.await必须在async定义的方法中使用

let e = await 78910;
console.log(e);

 

react 通过axios获取数据 出现promise为pending——使用await获取Promise对象的PromiseResult_微信

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();

react 通过axios获取数据 出现promise为pending——使用await获取Promise对象的PromiseResult_支付宝_02

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();

react 通过axios获取数据 出现promise为pending——使用await获取Promise对象的PromiseResult_支付宝_03

4.如果await右边为一个非Promise对象,则直接返回右边内容

async function main() {
let e = await 78910;
console.log(e);
}
main();

react 通过axios获取数据 出现promise为pending——使用await获取Promise对象的PromiseResult_微信_04

 






举报

相关推荐

0 条评论