Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/368.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 节点:使用promise.all()并行调用API?_Javascript_Node.js_Promise - Fatal编程技术网

Javascript 节点:使用promise.all()并行调用API?

Javascript 节点:使用promise.all()并行调用API?,javascript,node.js,promise,Javascript,Node.js,Promise,我有点困惑promise.all是如何工作的,它是否并行运行承诺数组 下面是一个示例代码 // index.js const getSomething = async (args) => { return await apiCallHere(args) } // Create Array of Promises const arrayOfPromises = sampleArray.map(sample => new Promise((resolve, reject) =&g

我有点困惑promise.all是如何工作的,它是否并行运行承诺数组

下面是一个示例代码

// index.js

const getSomething = async (args) => {
  return await apiCallHere(args)
}

// Create Array of Promises
const arrayOfPromises = sampleArray.map(sample => new Promise((resolve, reject) => {
      try {
        const something = this.getSomething(sample, args)
        resolve(something) 
      } catch (error) {
        reject(error)
      }
}))

await Promise.all(arrayOfPromises)

从我观察到的情况来看,Promise.all并行运行承诺,并等待所有承诺 结束

它是否并行运行一系列承诺

答应我,一切都不是,不;您的代码可能运行良好;请参见下面的注释。在承诺之前,工作已经在进行中。所有人都看到了承诺。承诺所做的就是给你一个承诺,当你给它的所有承诺都实现或其中一个被拒绝时,这个承诺就会解决

是您的代码使工作并行运行,方法是启动承诺在映射回调中报告完成的操作,以便首先将它们交给Promise.all。见***评论:

// *** `map` is synchronous, it loops all the way through the array
const arrayOfPromises = sampleArray.map(sample => new Promise((resolve, reject) => {
      try {
        const something = this.getSomething(sample, args) // *** This is what starts each thing
        resolve(something) 
      } catch (error) {
        reject(error)
      }
}))

// *** The work is already underway here

// *** This just waits for it to finish
await Promise.all(arrayOfPromises)
请记住,承诺只是观察异步过程完成的一种方式。承诺不会产生任何效果。他们只是报告某件事的完成情况,以及履行价值或拒绝原因

笔记 如果this.getSomethingsample、args返回一个承诺,那么您的代码将落入以下陷阱:根本没有理由在这里使用新承诺。相反:

const arrayOfPromises = sampleArray.map(sample => this.getSomething(sample, args));
如果this.getSomethingsample,args立即返回其值,那么在这里使用承诺就没有意义了,因为在返回时操作已经完成

我假设它不会启动一个异步进程,并通过回调而不是承诺来报告完成情况,因为您没有显示回调,但显示了使用返回值

问题中显示的getSomething返回一个promise,因为它是一个异步函数,但您不会将其称为this.getSomething…,就像getSomething

它是否并行运行一系列承诺

答应我,一切都不是,不;您的代码可能运行良好;请参见下面的注释。在承诺之前,工作已经在进行中。所有人都看到了承诺。承诺所做的就是给你一个承诺,当你给它的所有承诺都实现或其中一个被拒绝时,这个承诺就会解决

是您的代码使工作并行运行,方法是启动承诺在映射回调中报告完成的操作,以便首先将它们交给Promise.all。见***评论:

// *** `map` is synchronous, it loops all the way through the array
const arrayOfPromises = sampleArray.map(sample => new Promise((resolve, reject) => {
      try {
        const something = this.getSomething(sample, args) // *** This is what starts each thing
        resolve(something) 
      } catch (error) {
        reject(error)
      }
}))

// *** The work is already underway here

// *** This just waits for it to finish
await Promise.all(arrayOfPromises)
请记住,承诺只是观察异步过程完成的一种方式。承诺不会产生任何效果。他们只是报告某件事的完成情况,以及履行价值或拒绝原因

笔记 如果this.getSomethingsample、args返回一个承诺,那么您的代码将落入以下陷阱:根本没有理由在这里使用新承诺。相反:

const arrayOfPromises = sampleArray.map(sample => this.getSomething(sample, args));
如果this.getSomethingsample,args立即返回其值,那么在这里使用承诺就没有意义了,因为在返回时操作已经完成

我假设它不会启动一个异步进程,并通过回调而不是承诺来报告完成情况,因为您没有显示回调,但显示了使用返回值


问题中显示的getSomething返回一个承诺,因为它是一个异步函数,但您不会这样调用它。getSomething…,就像getSomething…

No.promise。all只需检查承诺数组并等待所有承诺都得到解决。在执行Array.prototype.map时,您已经在动态执行/运行承诺,并同时将它们推送到数组中。正如我所知,它没有在Parralel中运行,只是在您输入数组中的所有承诺时返回1个resolve promiseresolved@KhanhLeTran-不正确。异步操作确实在并行模资源争用中运行。否。all只需检查承诺数组并等待所有承诺都得到解决。在执行Array.prototype.map时,您已经在动态执行/运行承诺,并同时将它们推送到数组中。正如我所知,它没有在Parralel中运行,只是在您输入数组中的所有承诺时返回1个resolve promiseresolved@KhanhLeTran-不正确。异步操作确实在并行模资源争用中运行。谢谢,这是一个非常清楚的解释there@frostkazuma-不客气!请看我刚才添加到结尾的注释。我相信this.getSomethingsample,args应该返回一个承诺。感谢您注意到反模式,学到了很多,将更新我的代码!谢谢,这是一个非常清楚的解释,对吗there@frostkazuma-不客气!请看我刚才添加到结尾的注释。我相信this.getSomethingsample,args应该返回一个承诺。感谢您注意到反模式,学到了很多,将更新我的代码!