0

Why is my Aync Await Function returning an Pending Promise ? i already tried .then statement but it doesn't work, here is my code :

  const findData = async () => {
    let query = await userSchema.findOne({ _id: research["uploaderID"] });
    return query;
  };

  research["uploaderInfo"] = findData();

  console.log(findData());

when i tried to console.log the findData, it just gave me this :

Promise { <pending> }
Promise { <pending> }

and when i tried to check the research object, it was empty, but when i tried to add a console.log(query) inside the findData() function, it gave me the expected result, which mean the query is correct, and this is an issue because of the async / await.

UPDATE

i tried @dai solution to add await when i tried to set my research like this

  const findData = async () => {
    let query = await userSchema.findOne({ _id: research["uploaderID"] });
    return query;
  };

  async () => {
    research["uploaderInfo"] = await findData();
  };

when i tried this, any code that i put inside the second nameless async function does not work, i tried to set the object to random string and it still doesn't changing

Walls
  • 149
  • 13
  • 3
    Your `findData` function is an `async` function, which implicitly always returns a `Promise`, which means you need to `await` its returned `Promise` like so: `research["uploaderInfo"] = await findData();` – Dai Sep 03 '21 at 09:04
  • 1
    Yes, Agree with @Dai, Please Put await so you will get your answer. –  Sep 03 '21 at 09:07
  • it tells me that i need to put await inside an async function, do i need to put it inside a function ? @Dai – Walls Sep 03 '21 at 09:13
  • @Walls What version of NodeJS are you running? "top-level `await`" should be supported by modern versions of NodeJS AFAIK. Otherwise, see this: https://stackoverflow.com/questions/46515764/how-can-i-use-async-await-at-the-top-level – Dai Sep 03 '21 at 09:33
  • What do you mean any code you put inside the second async function does not work? Are you invoking that function? – user16708140 Sep 03 '21 at 09:38
  • @user16708140 inside the second async function, i tried to change the object value with the `findData()` using the `await`, but the data is not changing – Walls Sep 03 '21 at 09:39
  • @Dai im running the 14.17.xxx version of node that is reccomended for most users – Walls Sep 03 '21 at 09:39
  • But are you calling the second function? In here is just a function declaration – user16708140 Sep 03 '21 at 10:23
  • Where are you getting the `research` and `userSchema` objects from? Please post **all** your code (but keep "all code" to the bare minimum needed to reproduce the issue) – Dai Sep 03 '21 at 11:15

1 Answers1

1

Read this: https://stackoverflow.com/a/56590390/159145

Short answer: Run NodeJS 14.17 (or any version after 13.3+) with the flag --harmony-top-level-await and you can have this:

// program.js

const findData = async () => {
    let query = await userSchema.findOne({ _id: research["uploaderID"] });
    return query;
};

research["uploaderInfo"] = await findData();

console.log(research["uploaderInfo"]);

...or even just this (assuming your research and userSchema objects are trivially instantiated):

// program.js

research["uploaderInfo"] = await userSchema.findOne({ _id: research["uploaderID"] });

console.log(research["uploaderInfo"]);

Longer answer without NodeJS flags:

It looks like NodeJS 14.17 doesn't yet support "top-level await" (someone correct me if I'm wrong) but that's not a real problem: it just means you need to wrap all of your original top-level ("root function") code in an async function and invoke it immediately.

Like so (I've named the function entrypoint, though you can use an anonymous function if you like):

// program.js

async function entrypoint() {
    
    const research   = ...
    const userSchema = ...

    const findData = async () => {
        let query = await userSchema.findOne({ _id: research["uploaderID"] });
        return query;
    };
    
    research["uploaderInfo"] = await findData();

    console.log(research["uploaderInfo"]);
}

entrypoint();

Note that you can elide and inline findData's function and call userSchema.findOne directly:

// program.js

async function entrypoint() {

    const research   = ...
    const userSchema = ...    

    research["uploaderInfo"] = await await userSchema.findOne({ _id: research["uploaderID"] });

    console.log(research["uploaderInfo"]);
}

entrypoint();
Dai
  • 141,631
  • 28
  • 261
  • 374