0

I've got a pretty strong grasp of ES6, but I can't wrap my head around this:

How can I return from an async function from within a nested callback?

e.g.:

async function test () {
    someNonPromiseUsingFunction (data => return data)
}

as opposed to nesting everything in a promise:

function test () {
    return new Promise (resolve => someNonPromiseUsingFunction (data => resolve (data)))
}

I know it's not necessary, I just want to consistently use async and await in my code instead of mixing it with manual promise initializations.

Ethan McTague
  • 2,236
  • 3
  • 21
  • 53
  • TL;DR - `async` only works with Promises. If you want to use `async` functions, you will have to make sure that all of the values you want to `await` on are Promises. – Madara's Ghost Sep 25 '18 at 12:49
  • `async` gives you promise-`await`, not magical callbacks. You still need to promisify them as usual. – Bergi Sep 25 '18 at 14:58
  • In your particular example, you can simplify `data => resolve (data)` to just `return new Promise (resolve => someNonPromiseUsingFunction (resolve))`, or even `new Promise(someNonPromiseUsingFunction)` – Bergi Sep 25 '18 at 14:59

0 Answers0