0

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");
}
1
  • If getUserFromDb is asynchronous, then the two are not equivalent - async functions always return a promise, so in the last code, you are returning the promise and trying to get .Name from it, which doesn't exist on a promise. Commented Sep 17, 2019 at 14:04

4 Answers 4

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Correct me if I'm wrong but doesn't your second example return a promise that resolves with a promise?
@smolus - No. there's no such thing as a promise that is resolved with a promise. You can't do that. The promise infrastructure would just chain the two promises which would be invisible to the caller.
0

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).

Comments

0

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);
}

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.