Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript/Typescript如何并行运行异步函数?_Javascript_Typescript_Promise_Async Await - Fatal编程技术网

Javascript/Typescript如何并行运行异步函数?

Javascript/Typescript如何并行运行异步函数?,javascript,typescript,promise,async-await,Javascript,Typescript,Promise,Async Await,我有一段代码,它运行4个async函数: this.groupsLevels = await this.groupsLevelsService.getData(); this.indicators = await this.configurationService.getIndicators(); this.sources = await this.configurationService.getSources(); this.storeGroups = await this.storeGrou

我有一段代码,它运行4个
async
函数:

this.groupsLevels = await this.groupsLevelsService.getData();
this.indicators = await this.configurationService.getIndicators();
this.sources = await this.configurationService.getSources();
this.storeGroups = await this.storeGroupsService.getStoreGroups();

console.log(this.groupsLevels) // All of them is populated
console.log(this.indicators)   // All of them is populated
console.log(this.sources)      // All of them is populated
console.log(this.storeGroups)  // All of them is populated
如何并行运行所有4个?当所有这些都完成后,就可以得到最终的结果

我试过了

Promise.all([
  async () => {this.groupsLevels = await this.groupsLevelsService.getData(); },
  async () => {this.indicators = await this.configurationService.getIndicators(); },
  async () => {this.sources = await this.configurationService.getSources(); },
  async () => {this.storeGroups = await this.storeGroupsService.getStoreGroups(); },
]);

console.log(this.groupsLevels) 
console.log(this.indicators)   
console.log(this.sources)      
console.log(this.storeGroups)  
但没有人成功加载

我做错了什么?

你在找什么

[this.groupsLevels, this.indicators, this.sources, this.storeGroups] = await Promise.all([
  this.groupsLevelsService.getData(),
  this.configurationService.getIndicators(),
  this.configurationService.getSources(),
  this.storeGroupsService.getStoreGroups(),
]);

console.log(this.groupsLevels);
console.log(this.indicators);
console.log(this.sources); 
console.log(this.storeGroups);
你在找什么

[this.groupsLevels, this.indicators, this.sources, this.storeGroups] = await Promise.all([
  this.groupsLevelsService.getData(),
  this.configurationService.getIndicators(),
  this.configurationService.getSources(),
  this.storeGroupsService.getStoreGroups(),
]);

console.log(this.groupsLevels);
console.log(this.indicators);
console.log(this.sources); 
console.log(this.storeGroups);

您的问题已得到回答。:)您引用的答案不返回值。请注意,JS是单线程的,异步并不意味着并行返回承诺。所以你可以使用
。然后(()=>{})
。您可以将数据传递给变量,然后再访问这些变量。您的问题已经得到了回答。:)您引用的答案不返回值。请注意,JS是单线程的,异步并不意味着并行返回承诺。所以你可以使用
。然后(()=>{})
。你可以把你的数据传递给变量,然后你就可以访问这些变量了。我从未见过这个符号
[x,y,z]=…
它是什么意思?它是ES?@DanielSantos自ES6开始提供的阵列分解的一部分。更常用于变量声明,但您可以在元素中使用任何目标表达式:-)太好了。我从未见过这个符号
[x,y,z]=…
它是什么意思?它是ES?@DanielSantos自ES6开始提供的阵列分解的一部分。更常用于变量声明,但可以在元素中使用任何目标表达式:-)