1

If I have the following setup:

function entryPoint (someVariable) {

  getValue(arg)
    .then(anotherFunction)
}

function anotherFunction (arg1) {
}

How can I make someVariable available in anotherFunction?

Seth
  • 10,198
  • 10
  • 45
  • 68
Daniel
  • 1,284
  • 3
  • 12
  • 17

2 Answers2

1

You could try this.

function entryPoint (someVariable) {
  getValue(arg)
    .then(anotherFunction(someVariable))
}

function anotherFunction(someVariable) {
  return function(arg1) {
  }
}
Tesseract
  • 8,049
  • 2
  • 20
  • 37
  • I was wondering if there was some way to do it with bind, call, or apply? – Daniel Jan 24 '16 at 20:54
  • 2
    @Daniel you can, `.then(anotherFunction.bind(null,someVariable));`, note though you will have to modify the function definition ie `anotherFunction(someVar,arg1){}` as the bound variable will come before other passed arguments. – Patrick Evans Jan 24 '16 at 20:54
  • Binding and then reordering the arguments of anotherFunction worked. Thanks – Daniel Jan 24 '16 at 21:02
0

You can pass extra arguments with .bind, if you want to pass the context, don't use null and pass this or something else instead. But after you pass the context, pass the other values that you expect as params within anotherFunction.

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
}
Seth
  • 10,198
  • 10
  • 45
  • 68