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

172
Vistas
Stop/pause node tasks after executing them

If I wait for user input in node and depending on it I execute a function and pass some parameters inside it is there any way to stop this function after it starts being executed? I am creating a node app that runs multiple tasks depending on user input and I have no idea how people can "pause" or "stop" some tasks after they start being executed.

I saw some people creating some automation stuff and having stop/pause functionality but how exactly do they achieve it?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

We can do this by throwing an exception and catching after the main implementation. The app's state after stopping can be set in the catch block.

We can make a function that checks our stopped boolean, and throws an exception if it's true. We can then use it as a sort of checkpoint or stopping point throughout our function.

Here's an example:

Note that there are only two checkpoints. i.e. if it starts running Add(3) and you stop it, it'll still run Add(4)

let stopped = false;
stopButton.onclick = () => stopped = true;

const data = { result: 0 };

function checkpoint() {
  if (stopped) throw new Error();
}

async function add(data, num) {
  console.log(`Adding ${num}`);
  await new Promise((res) => setTimeout(res, 2000));
  data.result += num;
  return data;
}

(async () => {
  try {
    await add(data, 1);
    await add(data, 2);
    checkpoint();
    await add(data, 3);
    await add(data, 4);
    checkpoint();
    await add(data, 5);
    await add(data, 6);
    console.log('Done');
  } catch {
    console.log('Stopped running. Resetting program');
  }
})();
<button id="stopButton">Stop</button>

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