LastChar23
LastChar23

Reputation: 529

nodejs async functions with return statement

Currently this is my Code:

async function getUser(ID){
    //Code for Building SQL query
    return await getUserFromDb(query);
}

I just installed eslint and I read that it is useless to write "await" in a return Statement and that only slows down the function. After removing the await from the return row it says now that I dont have any await in my async function.

Do I still Need to make the function async? In my main function I call user = await getUser();. Do it be rigth to remove the await here and from the function? will it stil be ansyc?


So is this:

async function getUser(ID){
    //Code for Building SQL query
    return await getUserFromDb(query);
}

async function main(){
    console.log("User 1: test");
    console.log("User 2: " + await getUser(424).Name);
    console.log("User 3: test");
}

the same as this?:

function getUser(ID){
    //Code for Building SQL query
    return getUserFromDb(query);
}

async function main(){
    console.log("User 1: test");
    console.log("User 2: " + getUser(424).Name);
    console.log("User 3: test");
}

Upvotes: 0

Views: 1080

Answers (4)

jfriend00
jfriend00

Reputation: 708026

This:

async function getUser(ID){
    //Code for Building SQL query
    return await getUserFromDb(query);
}

or this:

async function getUser(ID){
    //Code for Building SQL query
    return getUserFromDb(query);
}

Are basically the same as this:

function getUser(ID){
    //Code for Building SQL query
    return getUserFromDb(query);
}

With one minor exception. If getUserFromDb() were to throw synchronously, then the async versions would catch that exception and turn it into a rejected promise. The non-async version would throw synchronously back to the caller.

There is no reason to do return await someFuncThatReturnsPromise(). You can just do return someFuncThatReturnsPromise() instead with no difference in outcome except more efficient code.

Do I still Need to make the function async?

Probably not. If getUserFromDb() is properly behaved and does not throw synchronously, then there is no reason to make the function async. You can just return the promise you already have.

In my main function I call user = await getUser();. Do it be right to remove the await here and from the function? will it stil be ansyc?

You will need to use either await getUser() or getUser().then(...) to get the value from getUser(). It's up to your own coding style preference which one you want to use. If you use await getUser(), then it has to be from an async function. If you use getUser().then(...), then the containing function does not need to be async.

Upvotes: 1

smolus
smolus

Reputation: 125

The correct version would actually be:

function getUser(ID){
    //Code for Building SQL query
    return getUserFromDb(query);
}

async function main(){
    let user = await getUser(424)
    console.log("User 1: test");
    console.log("User 2: " + user.Name);
    console.log("User 3: test");
}

since i assume "getUserFromDb" returns a promise (is asynchronous). The "getUser" function synchronously returns the promise from "getUserFromDb" and then you asynchronously await that promise in main.

Upvotes: 0

MarengoHue
MarengoHue

Reputation: 1825

If your async function just returns awaits a single promise and then returns it's result, its effectively the same as just making a function sync and then returning the promise. You would need to make the function async only if you had to wait for the inner promise to resolve and act on the result before returning, or if you need to await for multiple promises inside of it:

async function thisDoesntMakeSense() {
  return await someAsyncCall();
}

function thisDoes() {
  return someAsyncCall();
}

// Later down the line you can just await this call:
const result = await thisDoes();

async function thisIsProperUseOfAsync() {
  const asyncCallResult = await someAsyncCall();
  return asyncCallResult + 1; //Because you operate on the result of an async call
}

async function thisAlsoMakesSense() {
  const firstReslt = await firstAsyncCall();
  return await secondAsyncCall(firstResult);
}

Upvotes: 0

nicholaswmin
nicholaswmin

Reputation: 22989

This:

async function getUser(ID){
    //Code for Building SQL query
    return await getUserFromDb(query);
}

doesn't need neither async nor await.

Since you're already awaiting on getUserFromDb it is assumed it already returns a Promise, either explicitly or implicitly if it's an async-marked function.

So you can just do:

function getUser(ID){
    //Code for Building SQL query
    return getUserFromDb(query);
}

and then:

await getUser(ID).

Upvotes: 0

Related Questions