Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/456.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在ES6中,如何在.then()调用中向函数传递额外的参数?_Javascript_Ecmascript 6_Es6 Promise - Fatal编程技术网

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))