filicaliva
filicaliva

Reputation: 3

Using async-await in promises

.then(async (rows) => {
//some code
response = await sendEmail(email);
}

Hello, is it acceptable to make the then method in promises asynchronous if we refer to another interface to send email?

Upvotes: 0

Views: 93

Answers (2)

Pablo Gómez
Pablo Gómez

Reputation: 631

Agree with the answer from @derpirscher. The async/await syntax is mostly just syntactic sugar for promises but it can do wonders for legibility. Also you can try:

async function doSomething() {
let response;
try {
    response = await sendEmail(email);
} catch (err) {
    console.log(err);
    return []
}
return response

You can find a resource about async javascript and promises here.

Upvotes: 0

derpirscher
derpirscher

Reputation: 17372

While this would work, IMO it's bad style to mix async/await with promise chaining. Why not just

 fooPromise()
  .then(rows => {
    ...
    return sendEmail(email);
  })
  .then(response => {
    ...
  })

or

async function foo() {
   const rows = await fooPromise();
   ...
   const response = await sendEmail(email);
   ...
}

Ie, chose one way you like better and stick with it.

Upvotes: 1

Related Questions