Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

250
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda