面试题: 多个 await 处理,有一个失败,就算作失败

面试题多个 await 处理,有一个失败,就算作失败 ?

Promise.all([p1, p2, p3....])    // 返回的也是一个 Promise 对象 -------- asait异步成功返回一个数组[result, result]

解决: 一次发送多个请求,等所有请求都返回后再一起处理,如果一个请求出错,整个都会失败

  • Promise.all([]).then(results=>{
        // results 是一个 {status: 0, data:{....}} 数组 
    })
    
    // 或者
    const results = await Promise.all([reqCategory(pCategoryId), reqCategory(pCategoryId)]);

猜你喜欢

转载自www.cnblogs.com/tianxiaxuange/p/10297998.html