From f7c5b2c80f2e13a7d52edeaae4903bb9aff9f842 Mon Sep 17 00:00:00 2001 From: uditdey Date: Sat, 24 Oct 2026 16:04:01 +0000 Subject: [PATCH] Make stdlib classes uniform --- lib/stdlib/promise.js | 92 +++++++++++++++++++++---------------------- 1 file changed, 46 insertions(+), 46 deletions(-) diff --git a/lib/stdlib/promise.js b/lib/stdlib/promise.js index 045bb77..bcbbb94 100644 --- a/lib/stdlib/promise.js +++ b/lib/stdlib/promise.js @@ -20,17 +20,51 @@ const __PENDING = 0; const __FULFILLED = 1; const __REJECTED = 2; -class Promise { - constructor(executor) { - const p = { __proto__: this.__proto__, state: __PENDING, value: undefined, reason: undefined, resolved: false, reactions: [] }; +function Promise(executor) { + const p = { __proto__: this.__proto__, state: __PENDING, value: undefined, reason: undefined, resolved: false, reactions: [] }; - if (executor !== undefined) { - executor((value) => __resolve(p, value), (reason) => __reject(p, reason)); - } - - return p; + if (executor !== undefined) { + executor((value) => __resolve(p, value), (reason) => __reject(p, reason)); } + return p; +} + +Promise.resolve = function (value) { + if (__is_promise(value)) return value; + const p = { __proto__: this.prototype, state: __PENDING, value: undefined, reason: undefined, resolved: false, reactions: [] }; + __resolve(p, value); + return p; +}; + +Promise.reject = function (reason) { + const p = { __proto__: this.prototype, state: __PENDING, value: undefined, reason: undefined, resolved: false, reactions: [] }; + __reject(p, reason); + return p; +}; + +Promise.all = function (promises) { + const p = { __proto__: this.prototype, state: __PENDING, value: undefined, reason: undefined, resolved: false, reactions: [] }; + const values = []; + let left = promises.length; + + if (left === 0) __resolve(p, values); + + for (let i = 0; i < promises.length; i++) { + Promise.resolve(promises[i]).then( + (value) => { + values[i] = value; + left--; + if (left === 0) __resolve(p, values); + }, + (reason) => __reject(p, reason), + ); + } + + return p; +}; + +Promise.prototype = { then(onFulfilled, onRejected) { const derived = { __proto__: this.__proto__, state: __PENDING, value: undefined, reason: undefined, resolved: false, reactions: [] }; let value; @@ -68,11 +102,11 @@ class Promise { } return derived; - } + }, catch(onRejected) { return this.then(undefined, onRejected); - } + }, finally(f) { return this.then( @@ -85,42 +119,8 @@ class Promise { return Promise.reject(reason); }, ); - } - - static resolve(value) { - if (__is_promise(value)) return value; - const p = { __proto__: this.prototype, state: __PENDING, value: undefined, reason: undefined, resolved: false, reactions: [] }; - __resolve(p, value); - return p; - } - - static reject(reason) { - const p = { __proto__: this.prototype, state: __PENDING, value: undefined, reason: undefined, resolved: false, reactions: [] }; - __reject(p, reason); - return p; - } - - static all(promises) { - const p = { __proto__: this.prototype, state: __PENDING, value: undefined, reason: undefined, resolved: false, reactions: [] }; - const values = []; - let left = promises.length; - - if (left === 0) __resolve(p, values); - - for (let i = 0; i < promises.length; i++) { - Promise.resolve(promises[i]).then( - (value) => { - values[i] = value; - left--; - if (left === 0) __resolve(p, values); - }, - (reason) => __reject(p, reason), - ); - } - - return p; - } -} + }, +}; function __is_promise(x) { return typeof x === "object" && x !== null && x.reactions !== undefined;