当每个函数返回deferred.promise时,如何链接函数数组的执行? [英] How to chain execution of array of functions when every function returns deferred.promise?
问题描述
我使用延迟模块在Node.js中创建了我的第一个延迟对象,当我将结果传递给下一个函数并触发解析和拒绝时,它工作得很好。当每个函数返回deferred.promise时,如何链接执行函数数组?
我喜欢输入参数数组的函数和输入参数的第一个函数和每个下一个函数get参数。
I have created my first deferred object in Node.js using deferred module and it works great when I pass result to next function and trigger resolve and reject.How to chain execution of array of functions when every function returns deferred.promise ? I have like input parameters array of functions and input parameter for first function and every next function get parameter from previous.
它的工作方式类似于 f1(100).then(f2).then(f3)
,但是当我有多个函数时如何。
It works like f1(100).then(f2).then(f3)
, but how when I have n number of functions.
推荐答案
您需要在循环中构建一个承诺链:
You need to build a promise chain in a loop:
var promise = funcs[0](input);
for (var i = 1; i < funcs.length; i++)
promise = promise.then(funcs[i]);
这篇关于当每个函数返回deferred.promise时,如何链接函数数组的执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!