Javascript Promises,将额外的参数传递给 then chain
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32912459/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Promises, pass additional parameters to then chain
提问by user3110667
A promise, just for example:
一个承诺,例如:
var P = new Promise(function (resolve, reject) {
var a = 5;
if (a) {
setTimeout(function(){
resolve(a);
}, 3000);
} else {
reject(a);
}
});
After we call, then the method on the promise:
在我们调用之后,那么promise上的方法:
P.then(doWork('text'));
doWork function looks like this:
doWork 函数如下所示:
function doWork(data) {
return function(text) {
// sample function to console log
consoleToLog(data);
consoleToLog(b);
}
}
How can I avoid returning an inner function in doWork, to get access to data from the promise and text parameters? Are there any tricks to avoiding the inner function?
如何避免在 doWork 中返回内部函数,以从 promise 和 text 参数访问数据?有什么技巧可以避免内部函数吗?
采纳答案by thefourtheye
You can use Function.prototype.bind
to create a new function with a value passed to its first argument, like this
您可以使用Function.prototype.bind
传递给第一个参数的值来创建一个新函数,如下所示
P.then(doWork.bind(null, 'text'))
and you can change doWork
to,
你可以doWork
改为,
function doWork(text, data) {
consoleToLog(data);
}
Now, text
will be actually 'text'
in doWork
and data
will be the value resolved by the Promise.
现在,text
将是真正'text'
的doWork
和data
将被承诺解决的价值。
Note:Please make sure that you attach a rejection handler to your promise chain.
注意:请确保您将拒绝处理程序附加到您的承诺链。
Working program:Live copy on Babel's REPL
工作程序:在 Babel 的 REPL 上实时复制
function doWork(text, data) {
console.log(text + data + text);
}
new Promise(function (resolve, reject) {
var a = 5;
if (a) {
setTimeout(function () {
resolve(a);
}, 3000);
} else {
reject(a);
}
})
.then(doWork.bind(null, 'text'))
.catch(console.error);
回答by jib
Perhaps the most straightforward answer is:
也许最直接的答案是:
P.then(function(data) { return doWork('text', data); });
Or, since this is tagged ecmascript-6
, using arrow functions:
或者,由于这是标记为ecmascript-6
,因此使用箭头函数:
P.then(data => doWork('text', data));
I find this most readable, and not too much to write.
我觉得这最易读,而且不会写太多。
回答by yks
Use currying.
使用柯里化。
var P = new Promise(function (resolve, reject) {
var a = 5;
if (a) {
setTimeout(function(){
resolve(a);
}, 3000);
} else {
reject(a);
}
});
var curriedDoWork = function(text) {
return function(data) {
console.log(data + text);
}
};
P.then(curriedDoWork('text'))
.catch(
//some error handling
);
回答by JellyRaptor
Lodash offers a nice alternative for this exact thing.
Lodash 为这个确切的东西提供了一个不错的选择。
P.then(_.bind(doWork, 'myArgString', _));
//Say the promise was fulfilled with the string 'promiseResults'
function doWork(text, data) {
console.log(text + " foo " + data);
//myArgString foo promiseResults
}
Or, if you'd like your success function to have only one parameter (the fulfilled promise results), you can utilize it this way:
或者,如果您希望成功函数只有一个参数(履行的承诺结果),您可以这样使用它:
P.then(_.bind(doWork, {text: 'myArgString'}));
function doWork(data) {
console.log(data + " foo " + this.text);
//promiseResults foo myArgString
}
This will attach text: 'myArgString'
to the this
context within the function.
这将附加text: 'myArgString'
到this
函数内的上下文。