sync module

Provides an assortment of synchronisation primitives.

class PollPromise(func)

Runs a Promise-like function off the main thread until it is resolved through resolve or rejected callbacks. The function is guaranteed to be run at least once, irregardless of the timeout.

The func is evaluated every interval for as long as its runtime duration does not exceed interval. Evaluations occur sequentially, meaning that evaluations of func are queued if the runtime evaluation duration of func is greater than interval.

func is given two arguments, resolve and reject, of which one must be called for the evaluation to complete. Calling resolve with an argument indicates that the expected wait condition was met and will return the passed value to the caller. Conversely, calling reject will evaluate func again until the timeout duration has elapsed or func throws. The passed value to reject will also be returned to the caller once the wait has expired.


let els = new PollPromise((resolve, reject) => {
  let res = document.querySelectorAll("p");
  if (res.length > 0) {
  } else {
  • func (Condition) – Function to run off the main thread.
  • timeout (number) – timeout Desired timeout. If 0 or less than the runtime evaluation time of func, func is guaranteed to run at least once. The default is 2000 milliseconds.
  • interval (number) – interval Duration between each poll of func in milliseconds. Defaults to 10 milliseconds.

* – If func throws, its error is propagated.


Promise.<*> – Yields the value passed to func’s resolve or reject callbacks.

class TimedPromise(fn)

Represents the timed, eventual completion (or failure) of an asynchronous operation, and its resulting value.

In contrast to a regular Promise, it times out after timeout.

  • func (Condition) – Function to run, which will have its reject callback invoked after the timeout duration is reached. It is given two callbacks: resolve(value) and reject(error).
  • timeout (timeout) – timeout condition’s reject callback will be called after this timeout.
  • throws (Error) – throws When the timeout is hit, this error class will be thrown. If it is null, no error is thrown and the promise is instead resolved on timeout.

Promise.<*> – Timed promise.