简体   繁体   English

生成器+承诺并行化N个项目

[英]generators + promises to parallelize N number of items

the challenge: 挑战:

  1. We want to make N parallel ajax requests for an item's children. 我们要为项目的子项提出N个并行的ajax请求。
  2. Upon returning, we want to process them in sequential order (1...N) 返回后,我们要按顺序处理它们(1 ... N)
  3. We do NOT want to wait for all promises to return, but we want to process them IN ORDER as they come back. 我们不想等待所有的承诺都返回,但是我们希望在它们返回时按顺序进行处理。

For example: 例如:

Even if 2,3,5 come back before 1, we should hold onto the results of 2,3,5, and upon 1's return, process 1,2,3 in order (and wait for 4 to come back before 5) 即使2,3,5在1之前返回,我们也应保留2,3,5的结果,并在返回1时依次处理1,2,3(并等待4在5之前返回)

Tools: Q + ES6 generators 工具:Q + ES6生成器

Create array of N-1 length with placeholder variables 使用占位符变量创建N-1个长度的数组

EG when N = 3: N = 3时的EG

let [N1,N2,N3] = yield [ Promise1, Promise2, Promise3 ]

//process items sequentially:
   console.log(N1)
   console.log(N2)
   console.log(N3)

However, populating an array of empty variables doesn't seem to work of course because the reference doesn't know where to find the var declaration 但是,填充空变量数组似乎似乎不起作用,因为引用不知道在哪里可以找到var声明。

for(var i = 0; i< 3; i++) {
   res.push("some empty var")
}

Given the constraints of sticking to the tools provided, how could we parallelize calls, but process their returns sequentially? 考虑到使用所提供工具的限制,我们如何并行化调用,但按顺序处理它们的返回?

You can use Promise.all() , .then() 您可以使用Promise.all() .then()

javascript at Answer returns exact results described at Question 答案中的javascript返回问题中所述的确切结果

  1. We want to make N parallel ajax requests for an item's children. 我们要为项目的子项提出N个并行的ajax请求。
  2. Upon returning, we want to process them in sequential order (1...N) 返回后,我们要按顺序处理它们(1 ... N)
  3. We do NOT want to wait for all promises to return, but we want to process them IN ORDER as they come back. 我们不想等待所有的承诺都返回,但是我们希望在它们返回时按顺序进行处理。 how could we parallelize calls, but process their returns sequentially? 我们如何并行化调用,但顺序地处理它们的返回?

You can use .then() chained to original function which returns a promise or Promise object itself to process promise before return ing value to be processed in sequential order of parameters passed to Promise.all() at .then() chained to Promise.all() 您可以使用.then()链接到它返回一个承诺或原有功能Promise对象本身处理之前的承诺return以传递给参数顺序进行处理荷兰国际集团价值Promise.all().then()Promise.all()

 var n = 0; var fn = function() { return new Promise(function(resolve, reject) { console.log("promise " + ++n + " called"); setTimeout(function(i) { resolve(i) }, Math.random() * 2500, n) }) // handle requirement 3. here .then(function(res) { console.log(res); return res }) } Promise.all([fn(), fn(), fn()]) // handle requirement 1. here // handle requirement 2. here .then(function(data) { let [N1, N2, N3] = data; console.log(N1, N2, N3); }) 

You can do that by waiting for the next promise inside the loop: 您可以通过等待循环中的下一个承诺来做到这一点:

const promises = […]; // or created programmatically
for (const promise of promises) {
    const result = yield promise; // await them sequentially
    console.log(result);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM