6

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());
  • Maybe what you want to do is call `await getLastPage();` and only after that `return` ? – Nir Alfasi Aug 31 '18 at 17:29
  • It is hard to reproduce this with so many external dependencies. What value does `uri` have? Is Cheerio really required to reproduce the problem? Or the entire search thing? I cannot really run your code without having access to your computer, which makes debugging unnecessarily complicated. – Jörg W Mittag Aug 31 '18 at 17:32
  • @JörgWMittag raises a good point, Andrei - can you please write a [MCVE](https://stackoverflow.com/help/mcve) which doesn't take into account all the dependencies? it will make it much easier to help you! – Nir Alfasi Aug 31 '18 at 17:34
  • @JörgWMittag, The URI is okay, because it is working fine, using promise chaining, not async/await. The URI is: "https://spb.hh.ru/search/vacancy?text=node.js&area=2&page=0", i change it in the main post too. – Andrei Aleksandrov Aug 31 '18 at 17:39
  • 1) I wasn't arguing that there is a problem with that part of the code. But it prevents me from running the code, which means I cannot observe what happens and debug it. 2) If that part of the code is okay, then it cannot possibly have anything to do with the problem in your question, and is just a useless distraction that shouldn't even be in the question. – Jörg W Mittag Aug 31 '18 at 17:43
  • @Devon, let search = await rp({ uri: uri + 0, jar: true }); – Andrei Aleksandrov Aug 31 '18 at 17:43
  • @JörgWMittag i changed the code, as it said to be there – Andrei Aleksandrov Aug 31 '18 at 17:53
  • In what environment are you running your code? Perhaps Node.js? Please [edit] your question to indicate this information. It's probably a good idea to add it as a tag. – Makyen Aug 31 '18 at 18:54

1 Answers1

4

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.

Lennholm
  • 7,205
  • 1
  • 21
  • 30
  • I had always heard that await is blocking, but it turns out await only blocks the code execution within the async function... – aturc Oct 22 '20 at 18:40