延迟约束承诺

时间:2016-01-30 00:55:38

标签: javascript promise bind late-binding

我试图找出如何对promise函数参数进行后期绑定。在大多数情况下,我希望承诺流程只是一系列函数名称,因此它读起来就像一系列步骤(除非它像下面的单行一样)

(adjustedDate) => { aGreatFunctionNeedingADateInput.bind(undefined,adjustedDate) }

这是一个例子。该函数正在等待一个日期,因为它是晚期绑定输入,并且该输入将来自promise链中的某个步骤。

let aGreatFunctionNeedingADateInput = greatFunction.bind(undefined, x, y);
somePromise().then(functionThatMightChangeDate)
             .then( (adjustedDate) => {
                    aGreatFunctionNeedingADateInput.bind(undefined,adjustedDate)
             })                        
             .then(aGreatFunctionNeedingADateInput)

我知道这里有很多奇怪且可能错误的范围,但实质上我想将promise函数的最后一个参数绑定为最后一步,以防它在此过程中发生变化。如果可能,请告诉我。

更新:

在此链接中找到此示例:http://lhorie.github.io/mithril-blog/curry-flavored-promises.html

它实现了类似的结果,除了它在创建时绑定到固定值(John Doe)而不是像我在下面尝试的那样在promise链的中间。类似的想法,但我可以适应我的用途。

var createdBy = function(user, items) {
    return items.filter(function(item) {
        return item.createdBy == user
    });
};

m.request({method: "GET", url: "/api/projects"})
    .then(pastItems) // filter past projects
    .then(createdBy.bind(this, "John Doe")) // filter projects created by john doe
    .then(log) // log past projects created by john doe

1 个答案:

答案 0 :(得分:0)

你正在思考它。

“后期绑定”是我们通常所说的“传递”,因此不会绑定。

.then((adjustedDate) => aGreatFunctionNeedingADateInput(adjustedDate))

.then(aGreatFunctionNeedingADateInput)
相关问题