Promise.all奇怪的解决问题 [英] Promise.all strange resolution issue

查看:66
本文介绍了Promise.all奇怪的解决问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我打电话时请考虑这两个功能getStatusAll(data)-

Consider these two functions when i call getStatusAll(data)-

data=[[],['1'],['2'],['3']];

async function getStatusAll(data) {
    console.log("In getStatusAll");
  try{
    let statusPromiseArray =  data.map(async(value) => { 
      result= await this.fetchStatusDBs(value);
      return result;
    });
    statusResolvedArray= await Promise.all(statusPromiseArray)
    return statusResolvedArray;
  }catch(err){
    throw(err);
  }
}

async function fetchStatusDBs(data) {
    console.log("In fetchStatusDBs");
  try{
      //fetch status in dvf_req_id for an dvf_req_id
      if(data.length==0){
        console.log("1");
        dvfStatus = await Promise.resolve("Disabled");
        console.log("2");
        trainingStatus = await Promise.resolve("Disabled");
        console.log("3");
        inferenceStatus = await Promise.resolve("Disabled");
      }
      else {
        console.log("4");
        dvfStatus = await Promise.resolve("Enabled");
        console.log("5");
        trainingStatus = await Promise.resolve("Enabled");
        console.log("6");
        inferenceStatus = await Promise.resolve("Enabled");
      }
      return [dvfStatus,trainingStatus,inferenceStatus];
  }catch(err){
    throw(err);
  }
}

我正在尝试解决Promise.all中的多个Promise.但结果出乎意料.实际输出-

I am trying to resolve multiple Promises within a Promise.all but the results is unexpected. Actual Output-


In getStatusAll
In fetchStatusDBs
1
In fetchStatusDBs
4
In fetchStatusDBs
4
In fetchStatusDBs
4
2
5
5
5
3
6
6
6
[["Enabled","Enabled","Disabled"],["Enabled","Enabled","Enabled"],["Enabled","Enabled","Enabled"],["Enabled","Enabled","Enabled"]]

预期输出-

In getStatusAll
inside map
In fetchStatusDBs
1
2
3
inside map
In fetchStatusDBs
4
5
6
inside map
In fetchStatusDBs
4
5
6
inside map
In fetchStatusDBs
4
5
6
[["Disabled","Disabled","Disabled"],["Enabled","Enabled","Enabled"],["Enabled","Enabled","Enabled"],["Enabled","Enabled","Enabled"]]



但是像这样更改fetchStatusDB会以正确的格式返回输出.



But changing fetchStatusDBs like this returns output in the correct format.

async function fetchStatusDBs(data) {
    console.log("In fetchStatusDBs");
  try{
      if(data.length==0){
        dvfStatus = "Disabled";
        trainingStatus = "Disabled";
        inferenceStatus = "Disabled";
      }
      else {
        dvfStatus = "Enabled";
        trainingStatus = "Enabled";
        inferenceStatus = "Enabled";
      }
      return [dvfStatus,trainingStatus,inferenceStatus];
  }catch(err){
    throw(err);
  }
}

有人可以帮我吗?

推荐答案

您对async-await有一些误解

You have several misunderstandings about async-await

async function getStatusAll(data) {
    console.log("In getStatusAll");
  try{
    let statusPromiseArray =  data.map(async(value) => { // map is sync
      result= await this.fetchStatusDBs(value); // global result
      return result; // return-await anti-pattern
    });
    statusResolvedArray= await Promise.all(statusPromiseArray) // global
    return statusResolvedArray; // return-await anti-pattern
  }catch(err){  // catch-throw anti-pattern
    throw(err);
  }
}

所有这些都可以写成-

function getStatusAll(data) {
  return Promise.all(data.map(v => this.fetchStatusDBs(v)))
}

任何错误都会自动冒出.无需 catch 和重新 throw .这将完成 parallel 中的所有提取.您可以根据需要在 serial 中进行请求.这表明在 async -

And any error will automatically bubble up. No need to catch and re-throw. This will do all the fetches in parallel. You could do the requests in serial if you wanted. This is shown to demonstrate proper use of await in async -

async function getStatusAll(data) {
  const result = []
  for (const value of data)
    result.push(await this.fetchStatusDBs(value))
  return result
}

这篇关于Promise.all奇怪的解决问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆