0

I have a simple function that aims to log a user in, and some guard clauses in it to rule out errors

 async signIn(email: string, passwort: string): Promise<void> {
    const user: IPerson = await this.fetchUser(email);
    if (user === undefined) {
      return Promise.reject('Wrong email');
    }
    if (user.passwort !== passwort) {
      return Promise.reject('Wrong password');
    }
    if (!this.logInUser(user)) {
      return Promise.reject('Login Failed');
    }
    return Promise.resolve();
  }

I used async await to wait for the promise resolve of fetchUser giving me the user, but this made me think, what benefits does await have here?

As summarized in some blog this is how async works:

"Let’s emphasize: await literally suspends the function execution until the promise settles, and then resumes it with the promise result. That doesn’t cost any CPU resources, because the JavaScript engine can do other jobs in the meantime: execute other scripts, handle events, etc."

But in my case, there is no other other jobs in the meantime, the function can only resolve if the fetchUser provides something. Doesnt this mean JS Engine automatically waits and behaves just the same like using async? That would make it redundant.

WorldW1de
  • 71
  • 4
  • 1
    `return Promise.reject('Wrong email');`... or just `throw "Wrong email"` .... `return Promise.resolve();` or just `return` (in fact, at that point you don't even need `return`) – Jaromanda X Jul 19 '22 at 10:17
  • "*in my case, there is no other other jobs in the meantime*" - are you sure? How do you know? You've not shown us quite enough code or given any context to tell, but it's very unlikely there is nothing else going on in your application. – Bergi Jul 19 '22 at 13:24
  • [Don't reject promises with strings instead of proper errors!](https://stackoverflow.com/q/26020578/1048572) – Bergi Jul 19 '22 at 13:25

2 Answers2

0

what benefits does await have here?

It lets you write syntax that is easier to follow than using then with a callback.

That's all.


But in my case, there is no other other jobs in the meantime, the function can only resolve if the fetchUser provides something.

fetchUser is asynchronous. It isn't made asynchronous by the use of await. It provides a promise.

If you don't await that promise then you are just changing where the code execution is suspended.

Instead of signIn being suspended and the code that (you say) does nothing outside it continuing, you'll instead only suspend fetchUser and signIn will continue processing with the promise returned by fetchUser instead of the User object that that promise eventually resolves with.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • although, if i change my fetchUser to fetchUser(email: string): IPerson | undefined { return USERS.find((user: IPerson) => { return user.email === email; }); } i would not need await – WorldW1de Jul 19 '22 at 10:37
  • 1
    @WorldW1de — Yes, if you removed every thing you had which needed the value of the promise (like the password check) then you wouldn't need the tool that gets the value of the promise … but that's like saying you wouldn't need a seat belt if you got rid of the car. – Quentin Jul 19 '22 at 10:40
  • for me it works when using const user: IPerson | undefined = this.fetchUser(email); and then fetchUser is just returning the object of iperson. the code runs synchronous and everything works – WorldW1de Jul 19 '22 at 11:10
  • @WorldW1de — If that is the case then `fetchUser` doesn't return a promise (and you lied when you wrote "I used async await to wait for the promise resolve of fetchUser"). – Quentin Jul 19 '22 at 12:21
  • 1
    @WorldW1de In your question, you made us assume "*the promise resolve of fetchUser*". If `fetchUser` doesn't actually (need to) return a promise, that changes the question completely. – Bergi Jul 19 '22 at 13:28
0

Doesn't this mean JS Engine automatically waits and behaves just the same like using async? That would make it redundant.

No, the JS engine doesn't automatically "wait". Without await it would immediately continue executing the if statements, which obviously is not desired, as then you would not have the actual user object to work with, but "just" a promise object.

If the only thing that you wanted to do was to call fetch then it would be redundant to await it, but since you need the response from that request, you need to get informed when that response is available, and that is what await is offering you. You could alternatively chain a then call, whose callback would be called when the response becomes available.

trincot
  • 317,000
  • 35
  • 244
  • 286