Skip to main content

Barrier

import { Barrier } from '@dr.pogodin/react-utils';

The Barrier class is just an auxiliary wrapper around Promise which has its resolution and rejection functions attached as methods to the class instances. It sometimes comes handy for synchronisation between different asynchronous operations.

info

The Barrier source code has been moved to JS Utils library, and it is re-exported from there; thus, it also can be imported as

import { Barrier } from '@dr.pogodin/js-utils';

Fields

  • .resolvedboolean — Readonly. true once the barrier has been resolved; false before it.
  • .rejectedboolean — Readonly. true once the barrier has been rejected; false before it.
  • .settledboolean — Readonly. true once the barrier has been resolved or rejected; false otherwise.

Methods

constructor()

const barrier = new Barrier(executor);

Creates a new Barrier.

Arguments

  • executor - function - Optional. When provided, it will be called with resolve and reject arguments as the usual Promise constructor does. As Barrier instances have resolve() and reject() methods attached, the executor is not necessarily required.

catch()

barrier.catch(onRejection): Barrier;

Similar to the standard Promise catch(), but returns a Barrier which resolve() and reject() methods act on the original barrier.

finally()

barrier.finally(onFinally): Barrier;

Similar to the standard Promise finally(), but returns a Barrier which resolve() and reject() methods act on the original barrier.

resolve()

barrier.resolve(value);

Resolves the barrier with the optional value, similar to how the standard Promise resolves.

reject()

barrier.reject(value);

Rejects the barrier with the optional value, similar to how the standard Promise rejects.

then()

barrier.then(onResolved, onRejected): Barrier;

Similar to the standard Promise then(), but returns a Barrier which resolve() and reject() methods act on the original barrier.