面试题: 多个 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)]);
相关文章
相关标签/搜索