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
.resolved
— boolean — Readonly. true once the barrier has been resolved; false before it..rejected
— boolean — Readonly. true once the barrier has been rejected; false before it..settled
— boolean — Readonly. true once the barrier has been resolved or rejected; false otherwise.
Methods
- constructor() - Creates a new Barrier.
- catch() - Similar to a Promise catch().
- finally() - Similar to a Promise finally().
- resolve() - Resolves the barrier.
- reject() - Rejects the barrier.
- then() - Similar to a Promise then().
constructor()
const barrier = new Barrier(executor);
Creates a new Barrier.
Arguments
executor
- function - Optional. When provided, it will be called withresolve
andreject
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
.