Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

async/await return Promise { <pending> } [duplicate]

my question is: why does this log 'promise {pending}' despite of i used async/await? I checked similar questions and answers on them and it seems like it should be okay but it is not. How do i change it to get the result and why? Thank you.

const rp = require('request-promise-native');

async function sampleFunc() {
    let sample = await rp({
        uri: 'http://google.com',
        jar: true
    });
    return sample;
}

async function f() {
    return await sampleFunc();
}

console.log( f());
like image 306
Andrei Aleksandrov Avatar asked Aug 31 '18 17:08

Andrei Aleksandrov


People also ask

Does async await always return promise?

The word “async” before a function means one simple thing: a function always returns a promise. Other values are wrapped in a resolved promise automatically. So, async ensures that the function returns a promise, and wraps non-promises in it.

Is return await redundant?

When to use return await. return await should only be used inside a try… catch as it is redundant otherwise. This can be enforced with the ESLint rule no-return-await .

How do I return async await promise?

Inside an async function, you can use the await keyword before a call to a function that returns a promise. This makes the code wait at that point until the promise is settled, at which point the fulfilled value of the promise is treated as a return value, or the rejected value is thrown.

Why is my asynchronous function returning promise pending instead of a value?

async functions always return promises. you need to await to get the value. run() var result = data // How to assign resultant value to another variable? no, it won't output what you want to myVar , because run is marked async , so it returns a Promise .


1 Answers

async-await is really just syntactic sugar for promises, it doesn't make asynchronous code run synchronously (i.e. it doesn't block code execution). Your code is equivalent to the following:

function sampleFunc() {
    return new Promise(function(resolve) {
        rp({
            uri: 'http://google.com',
            jar: true
        }).then(function(sample) {
           resolve(sample);
        });
    });
}

function f() {
    return new Promise(function(resolve) {
        sampleFunc().then(function(result) {
            resolve(result);
        });
    });
}

console.log( f());

(Yes, I know the above code demonstrates an anti-pattern. It's for illustrative purposes)

As you can see, what an async function really does is to implicitly return a promise that gets resolved with the value you eventually return inside the function (or rejected if you throw something). This is why async-await can only be used on functions and thus isn't applicable on the top level.

The context that called the function is entirely agnostic to the fact that the function is async, it just calls the function, gets a promise back and moves on to the next line of code. Your console.log() lives in this outside context and only sees the promise that was returned.

like image 170
Lennholm Avatar answered Oct 26 '22 13:10

Lennholm