0
.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?

1
  • Yes, this is like returning a promise for the chain. It's fine. Although I would check why you need to mix .then. It's usually easier to stick to one or the other promise api Commented Jul 8, 2021 at 10:58

2 Answers 2

1

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.

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

Comments

0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.