0

i'm a little bit confused how i'm gonna approach my problem and whats the best practise is.

On my node server i have this post function:

router.post('/addUser', (req, res, next) => {
    const newUser = new User({
       userName: req.body.userName
       userPicture: req.body.profilePicture
       personLookALike: -> functionThatReturnsAObjectAsynchronously() <-
       ...
    })
}

The problem is, i want to save the post so i normally would have this later in the code:

newUser.save()

But newUser.save() gets executed first before the Object is fully finished(the "personLookALike" property is missing)

I know i could wrap this all with a promise and then put the save proces in a then() block but i have more of these asynchronous function in the object creation.

What is the best practise for my approach? Thank you!

andy_f96
  • 7
  • 2
  • 1
    If you have multiple of these, use multiple `then` calls in a chain :-) And once having understood how those work, switch to `async`/`await`. – Bergi Jun 25 '20 at 21:05

2 Answers2

0

This seems like an ideal use case for async/await:

router.post('/addUser', async (req, res, next) => {
    const personLookAlike = await functionThatReturnsAObjectAsynchronously();
    const newUser = new User({
       userName: req.body.userName,
       userPicture: req.body.profilePicture,
       personLookALike,
       ...
    })
    newUser.save();
}
Robin Zigmond
  • 17,805
  • 2
  • 23
  • 34
  • 1
    Of course. But the OP wasn't asking about error handling, nor did they give any insight into what sort of errors might be thrown by the async function being called. I'm assuming this code is sort of "schematic" and will need to be filled in with more details. – Robin Zigmond Jun 25 '20 at 21:10
  • If the OP didn't think about error handling, he won't fill it in either, so it's better if your answer already includes it in the schematic. Using `async` functions as callbacks (and having to call a `next` callback) is pretty hard to get right. I even recommend to [let a separate wrapper deal with it](https://stackoverflow.com/a/41350872/1048572). – Bergi Jun 25 '20 at 21:27
  • Ohhhh i didnt know i have to put the "async" tag before (req, res, next)! I've tried it with async but i handled it wrong, thanks! Error handling would be in the promise function, in my in casefunctionThatReturnsAObjectAsynchronously() with catch() right? – andy_f96 Jun 25 '20 at 21:32
0

functionThatReturnsAObjectAsynchronously is doing asynchronous work, so you want to do "pause" your function until the async function settles using await and then continue with your function.

For example:

router.post('/addUser', /* NOTICE: */ async (req, res, next) => {
    const newUser = new User({
       userName: req.body.userName
       userPicture: req.body.profilePicture
       personLookALike: /* NOTICE: */ await functionThatReturnsAObjectAsynchronously()
       ...
    })
}

The above code assumes functionThatReturnsAObjectAsynchronously returns a Promise and error handling should be taken care of.

You should further read about Promises, async functions and await

Arik
  • 5,266
  • 1
  • 27
  • 26