Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

240
Views
call multiple async functions in sequential order
async function unsubscribeUserHandler() {
  const unsubscribe = await fetch("/api/stripe-sessions/cancel-subscription", {
    method: "PATCH",
    body: JSON.stringify(),
    headers: {
      "Content-Type": "application/json",
    },
  });
  const data = await unsubscribe.json();
  if (!unsubscribe.ok) {
    Toast.fire({
      icon: "error",
      title: `${data.message}`,
    });
  } else {
    Toast.fire({
      icon: "success",
      title: `${data.message}`,
    });
  }
}

async function deleteUserHandler() {
  const deleteUser = await fetch("/api/user/delete-account", {
    method: "DELETE",
    body: JSON.stringify(),
    headers: {
      "Content-Type": "application/json",
    },
  });
  const data = await deleteUser.json();
  if (!deleteUser.ok) {
    Toast.fire({
      icon: "error",
      title: `${data.message}`,
    });
  } else {
    Toast.fire({
      icon: "success",
      title: `${data.message}`,
    });
  }
}

const deleteAccount = async () => {
    try {
        await unsubscribeUserHandler();
        await deleteUserHandler();
    } catch (err) {
        console.error('ERROR@@@@@!!!',err);
    }
}

const Settings = () => {
  return <DeleteAccount onDeleteAccount={deleteAccount} />;
};

As shown here, I want to unsubscribe.. only after the unsub, then run delete handler.

I have issues where It only runs one of the handlers and not the other. Are there other ways to do this?

have tried:

.then(() => deleteUserHandler())

and

.then(deleteUserHandler)

above doesn't make call to /api/user/delete-account,

only to unsubscribe.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

This is wrong:

const deleteAccount = () => unsubscribeUserHandler()
    .then(deleteUserHandler())
    .catch(console.error);

You aren't passing deleteUserhandler to then(), you are immediately calling it and pass the result to then().

To fix, lose the parenthesis:

const deleteAccount = () => unsubscribeUserHandler()
    .then(deleteUserHandler)
    .catch(console.error);

Or use an arrow function:

const deleteAccount = () => unsubscribeUserHandler()
    .then(() => deleteUserHandler())
    .catch(console.error);

Or better yet:

const deleteAccount = async () => {
  try {
    await unsubscribeUserHandler();
    await deleteUserHandler();
  } catch (err) {
    console.error(err);
  }
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!