1

Should be a fairly simple and quick question, as I am new to using Promises and await/async

This is my original function with Promises. (I want the res.send and res.redirect to happen after the user is created)

router.post('/add', (req, res) => {
 const user = req.body.user;
 if (user.email && user.username && user.password) {
 User.create(createUserObject(user)).then(
 res.send(user);
 res.redirect('/login'));
 }
});

This is my new function with await/async.

router.post('/add', async (req, res) => {
 const user = req.body.user;
 if (user.email && user.username && user.password) {
 await User.create(createUserObject(user));
 await res.send(user);
 await res.redirect('/login');
 }
});

Is it correct that I need await on every line to make the code function the same? I am concerned that I am using await where I don't need to.

asked Nov 18, 2019 at 20:08
3
  • you need to use await wherever you're waiting for data. Like api calls etc. So no , not everywhere Commented Nov 18, 2019 at 20:12
  • I recommend switching to TypeScript so you know for sure when a method returns a Promise<T> and when it doesn't. That said, using await with a non-Promise will work. Commented Nov 18, 2019 at 20:14
  • "I am concerned that I am using await where I don't need to." Then read about what await does: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… . async/await is syntactic sugar to work with promises. await only makes sense to use if the value you are trying to await is a promise. Commented Nov 18, 2019 at 20:16

3 Answers 3

2

I think this should work

 router.post('/add', async (req, res) => {
 const user = req.body.user;
 if (user.email && user.username && user.password) {
 await User.create(createUserObject(user));
 res.send(user);
 res.redirect('/login');
 }
 });

The only time you're waiting for something is when you wait for the user to be added to the db. So, wherever you wait for something you'll typically get a promise returned. That's where you can use await.

answered Nov 18, 2019 at 20:15
Sign up to request clarification or add additional context in comments.

6 Comments

I was thinking of this, but does this ensure that res.send and res.redirect are called only after the User is created?
@AndrewZaw: If res.send(user); and res.redirect('/login'); are currently inside the .then callback (your first example is invalid so we don't know) and it works correctly, then yes, this await version will be equivalent.
yes, because those are synchronus functions. Which means they run from top to bottom. They run as long as long as they got their parameters. If the parameters were asynchronus you would have to put await infront of the params.
@Kevin.a: Nitpick: There is no such thing as "asynchronous parameters". I assume you mean that if a parameter is promise then it can/has to be awaited.
@Kevin.a: I get it, but I want to avoid confusing people who just get started with async stuff in JavaScript. By introducing your own terminology it's easy to make things appear more complicated than it really is.
|
0

Is it correct that I need await on every line to make the code function the same?

No. You can do, but you don't need to.

You only need to use await when a function returns a Promise<T>. If a function returns a "normal" value (a string, number, Object, etc) then the extra await will still work but is not needed.

answered Nov 18, 2019 at 20:16

Comments

0

You don't need to put await in front of every function call, only the ones that are asynchronous and return a promise.

Assuming the line User.create(createUserObject(user)) is the only asynchronous call, that should be the only line that needs an await.

On the other hand, putting await in front of every function call may not harm your code other than making it look more cluttered.

answered Nov 18, 2019 at 20:16

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.