Skip to content Skip to sidebar Skip to footer

Returning An Awaited Value Returns A Promise? (es7 Async/await)

const ret = () => new Promise(resolve => setTimeout( () => resolve('somestring'), 1000)); async function wrapper() { let someString = await ret(); return someStri

Solution 1:

Async functions return promises. In order to do what you want, try something like this

wrapper().then(someString => console.log(someString));

You can also await on wrapper() like other promises from the context of another async function.

console.log(awaitwrapper());

Solution 2:

if you want your async function to return a value immediatly you can use Promise.resolve(theValue)

asyncwaitForSomething() {
    const somevalue = awaitwaitForSomethingElse()
    console.log(somevalue)

    returnPromise.resolve(somevalue)
}

IMO the async await keywords need one more, resolve

it would be nice to write return resolve 'hello'

or just

resolve 'hello'

Post a Comment for "Returning An Awaited Value Returns A Promise? (es7 Async/await)"