You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
Kuba Orlik ee3d998c08 Resolve with the final value 4 years ago
src Resolve with the final value 4 years ago
.gitignore Type corrections 4 years ago
README.md Update README 4 years ago
package.json Type corrections 4 years ago
tsconfig.json Initial commit 4 years ago

README.md

Abortable promises

A module for node that lets you run complex sequence of async steps and:

  1. Use an external signal to stop the sequence of steps
const sleep = promisify((timeout: number, callback: (...args: any[]) => void) =>
  setTimeout(callback, timeout)
);

const a = new AbortablePromise(async function* () {
  yield await sleep(1000);
  console.log("awaited 100");
  yield await sleep(2000);
  console.log("awaited 200");
  yield await sleep(3000);
  console.log("awaited 300");
});

setTimeout(() => a.abort(), 1500); // doesn't get to sleep(3000)
  1. Use an external signal to kill an already started asynchronous step
const b = new AbortablePromise(async function* (await_or_abort) {
  const ping = await deferedSpawn("ping", ["8.8.8.8"]);
  while (true) {
    console.log(
      await await_or_abort(ping.waitForNextData(), () => {
        ping.kill();
      })
    );
    yield;
  }
});

setTimeout(() => b.abort(), 5000); // doesn't get to 5th ping