1

I have a JS promise with a asynchronous function inside of it. How can I resolve or reject the function from inside the asynchronous function? Here is an example code...

    let promise = new Promise(function (resolve, reject) {

        asynchronousFunction();

      }).then(function (response) {
        //...
      });

   function asynchronousFunction() {

    //mimic asynchronous action...
    setTimeout(function(){
     resolve()
    },1000)

  }
nachshon f
  • 3,540
  • 7
  • 35
  • 67
  • 1
    Pass `resolve` (and `reject`) to the function as arguments. Then you can even just do `new Promise(asynchronousFunction)`. Or make `asynchronousFunction` return a promise to begin with. – Felix Kling May 03 '18 at 21:49
  • 2
    Or make your `asynchronousFunction` also return a promise. – Intervalia May 03 '18 at 21:50
  • 1
    Can you give a more detailed answer? – nachshon f May 03 '18 at 21:50
  • *"Then what?"* Then you call it as shown in your example. Not sure what you are confused about: `function asynchronousFunction(resolve, reject) { ... do stuff ... resolve(someValue); }; new Promise(asynchronousFunction)`. But simply making `asynchronousFunction` return a promise would be the better solution. – Felix Kling May 03 '18 at 21:50
  • Should I return the resolve()? – nachshon f May 03 '18 at 21:54
  • 1
    `resolve` doesn't return anything, so there is no point in doing that. – Felix Kling May 03 '18 at 21:55
  • 1
    `asynchronousFunction()` should return it's own promise when it's done with it's async activity. Then, the caller can just use that promise rather than create their own wrapper promise. Promises work best when each low level async operation is coded to return it's own promise. Then, you can use various promise capabilities to coordinate promises using chaining, branching or `Promise.all()`. Don't make an async function that tries to resolve some parent promise. Give it its own promise. – jfriend00 May 03 '18 at 21:59

1 Answers1

5

Your async function needs some way of letting the outside world know when it has something to report. Some async functions take a callback. If that's the case, you could pass a callback that calls your promise's resolve():

let promise = new Promise(function(resolve, reject) {
  asynchronousFunction((val) => { // < -- pass a callback into the function
    resolve(val)
  });
}).then(function(response) {
  console.log("recieved: ", response)
});

function asynchronousFunction(cb) {
  //mimic asynchronous action...
  setTimeout(function() {
    cb("some return value") // <-- call the callback here withthe return value
  }, 1000)

}

If you are in control of the async function, you should just have it return a promise directly:

function asynchronousFunction() {
  //mimic asynchronous action...
  return new Promise(function(resolve, reject) {
    setTimeout(function() {
      resolve("some return value")  withthe return value
    }, 1000)
  })
}

// call it:
asynchronousFunction()
  .then(val => console.log(val))
Mark
  • 90,562
  • 7
  • 108
  • 148