Skip to content

Commit

Permalink
Add finalizedResult prop and update then method
Browse files Browse the repository at this point in the history
  • Loading branch information
timursevimli committed Feb 21, 2024
1 parent 8f43027 commit e219d1a
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions lib/collector.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class Collector {
#controller = null;
#signal = null;
#timeout = null;
#finalizedResult = null;

constructor(keys, options = {}) {
const { exact = true, reassign = false } = options;
Expand Down Expand Up @@ -107,17 +108,19 @@ class Collector {
}

then(onFulfilled, onRejected = null) {
return new Promise((resolve, reject) => {
this.#fulfill = resolve;
this.#reject = reject;
if (!this.done) return;
if (this.#cause) return void reject(this.#cause);
if (!this.validate) return void resolve(this.data);
Promise.resolve(this.validate(this.data)).then((result) => {
if (result) resolve(result);
else resolve(this.data);
}, reject);
}).then(onFulfilled, onRejected);
if (!this.#finalizedResult) {
this.#finalizedResult = new Promise((resolve, reject) => {
this.#fulfill = resolve;
this.#reject = reject;
if (!this.done) return;
if (this.#cause) return void reject(this.#cause);
if (!this.validate) return void resolve(this.data);
Promise.resolve(this.validate(this.data)).then((res) => {
resolve(res ?? this.data);
}, reject);
}).then(onFulfilled, onRejected);
}
return this.#finalizedResult;
}
}

Expand Down

0 comments on commit e219d1a

Please sign in to comment.