Javascript 在ES6中,如何在.then()调用中向函数传递额外的参数?
如果我有以下设置:Javascript 在ES6中,如何在.then()调用中向函数传递额外的参数?,javascript,ecmascript-6,es6-promise,Javascript,Ecmascript 6,Es6 Promise,如果我有以下设置: function entryPoint (someVariable) { getValue(arg) .then(anotherFunction) } function anotherFunction (arg1) { } 如何使某个变量在另一个函数中可用?您可以试试这个 function entryPoint (someVariable) { getValue(arg) .then(anotherFunction(someVariable))
function entryPoint (someVariable) {
getValue(arg)
.then(anotherFunction)
}
function anotherFunction (arg1) {
}
如何使某个变量
在另一个函数
中可用?您可以试试这个
function entryPoint (someVariable) {
getValue(arg)
.then(anotherFunction(someVariable))
}
function anotherFunction(someVariable) {
return function(arg1) {
}
}
可以使用
.bind
传递额外参数,如果要传递上下文,请不要使用null
并传递此
或其他内容。但是,在传递上下文后,将期望的其他值作为参数传递到另一个函数中
function entryPoint (someVariable) {
getValue(arg)
.then(anotherFunction.bind(null, 1, 2))
}
function anotherFunction (something, other, arg1) {
// something = 1
// other = 2
// the returned value from the promise will be set to arg1
}
我想知道是否有办法使用bind、call或apply来实现它?@Daniel you can,
。然后(anotherFunction.bind(null,someVariable))代码>,请注意,尽管您必须修改函数定义,即另一个函数(someVar,arg1){}
,因为绑定的变量将位于其他传递的参数之前。绑定然后对另一个函数的参数重新排序是有效的。感谢使用正常的闭包:getValue(arg)。然后(result=>anotherFunction(someVariable))