简体   繁体   English

承诺:将新参数传递给链序列中的项目

[英]Promises: Passing new Parameters to item in Chain Sequence

I'm using the q library for promises . 我正在使用q库来保证 I'm trying to chain multiple functions that return promises together in a sequence, but I want to pass new parameters in. 我正在尝试将按顺序返回promise的多个函数链接在一起,但是我想传入新参数。

eg I want to do this pseudo-code: 例如我想做这个伪代码:

var func1Param = 'abc';
var func2Param = 'def';
var func3Param = 'ghi';

func1(func1Param).then(func2(func2Param)).then(func3(func3Param));

So far to handle this, I've been doing something like the following, and then having the first function have all the parameters, and pass them on to the second function with the resolve, and so forth. 到目前为止,我一直在做类似以下的事情,然后让第一个函数具有所有参数,然后将它们传递给带有resolve的第二个函数,依此类推。 eg 例如

var parms = { func1 : 'abc', func2: 'def', func3: 'ghi' }
func1(params).then(func2).then(func3);

However, this seems sloppy to pass parameters to a function that doesn't need them to get those parameters to a function that does need them. 但是,将参数传递给不需要它们的函数似乎很草率,无法将参数传递给需要它们的函数。

What is the best way to do this? 做这个的最好方式是什么? Every option I come up with either doesn't work, or seems kind of crazy convoluted. 我想出的每个选项都不起作用,或者看起来有些令人费解。 There has to be a simple way to do this properly. 必须有一种简单的方法来正确执行此操作。

I'm not sure if I understand this correctly, but if you want to add default/prespecified variables to (Promise) callbacks you can use Function.bind(thisArg[, arg1[, arg2[, ...]]]) 我不确定我是否理解正确,但是如果要将默认/预定变量添加到(承诺)回调中,则可以使用Function.bind(thisArg[, arg1[, arg2[, ...]]])

Eg 例如

function1(foo)
    .then(function2.bind(this, bar)
    .then(function3.bind(this, baz)

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

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