Skip to main content

Barrier

import { Barrier } from '@dr.pogodin/react-utils'; // or from '@dr.pogodin/js-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.

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): Barrier;

Resolves the barrier with the optional value, similar to how the standard Promise resolves. To allow chaining, it returns this barrier instance.

reject()

barrier.reject(value): Barrier;

Rejects the barrier with the optional value, similar to how the standard Promise rejects. To allow chaining, it returns this barrier instance.

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.