0.0.12 • Published 6 years ago

@tboyt/coroutine-manager v0.0.12

Weekly downloads
2
License
MIT
Repository
-
Last release
6 years ago

CoroutineManager

Module used in Pearl to manage coroutines as part of its run loop.

Coroutines are generators that yield promises. They can also yield nothing, to indicate that they should just run on the next tick.

Coroutines differ from traditional async functions in that their execution is tied into runloop ticks. When a promise resolves, rather than resume on the next environment-internal runloop tick like a regular async function, they will not resume until the next userland tick.

In addition, coroutines can be canceled with the cancel() method. While this will not cancel the yielded promise, it will prevent the coroutine from continuing execution when the promise resolves.

Example usage

import CoroutineManager from '@tboyt/coroutine-manager';

const manager = new CoroutineManager();

const timeout = (ms: number) =>
  new Promise((resolve, reject) => {
    setTimeout(resolve, ms);
  });

manager.run(function*() {
  console.log('running with timeout');
  yield timeout(500);
  console.log('done!');
});

const coroutine = manager.run(function*() {
  console.log('running with timeout, but canceling');
  setTimeout(() => manager.cancel(coroutine), 100);
  yield timeout(500);
  console.log('this never gets run!');
});

setInterval(() => manager.tick(), 100);
0.0.12

6 years ago

0.0.11

6 years ago

0.0.8

6 years ago

0.0.7

6 years ago

0.0.6

6 years ago

0.0.5

6 years ago

0.0.3

6 years ago

0.0.2

6 years ago

0.0.1

6 years ago