birdofpreyru/js-utils

Improve `Barrier`

Closed this issue · 3 comments

Resolver and rejecter should return the barrier itself, to allow chaining.

type Resolver<T> = (value: T | PromiseLike<T>) => void;
type Rejecter = (reason?: any) => void;

Also, perhaps check if the barrier has been resolved already with given result, and make it noop, or fail if it has been resolved or rejected with a different result.

The first take on it caused a critical bug in host code (see #10). The change has been reverted for now, but upon a post-mortem investigation, although I am not able to reproduce the problem in Jest tests in the repo, it looks like the problem can be avoided by ensuring that versions of resolveRef and rejectRef passed into executor here:

if (executor) executor(resolveRef, rejectRef);
do not return the barrier instance.

Although, pay attention that here:

js-utils/src/Barrier.ts

Lines 69 to 70 in ffd9f37

res.p_resolve = this.resolve;
res.p_reject = this.reject;
the attached resolved and reject should be wrapped, I guess, to return instance of res rather than this.

Also, perhaps, revisit the typing, rely on the standard Promise types?

The second take on this ticket is released as v0.0.12, to be verified it does not break RN environment, similar to #10.

Seems to work fine now.