From 6ac612df46621b3e3ce96ed0795de3da5db17c5e Mon Sep 17 00:00:00 2001 From: 0PrashantYadav0 Date: Mon, 10 Aug 2026 19:25:37 +0530 Subject: [PATCH 1/9] feat: implement Promise polyfill with resolve, reject, and state management --- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown_pkg_readmes status: na - task: lint_markdown_docs status: na - task: lint_markdown status: na - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: na - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: na - task: lint_license_headers status: passed --- --- .../@stdlib/promise/polyfill/lib/fulfill.js | 52 ++++ .../@stdlib/promise/polyfill/lib/fulfilled.js | 35 +++ .../@stdlib/promise/polyfill/lib/handle.js | 70 ++++++ .../@stdlib/promise/polyfill/lib/index.js | 50 ++++ .../@stdlib/promise/polyfill/lib/main.js | 231 ++++++++++++++++++ .../@stdlib/promise/polyfill/lib/pending.js | 35 +++ .../@stdlib/promise/polyfill/lib/reject.js | 52 ++++ .../@stdlib/promise/polyfill/lib/rejected.js | 35 +++ .../@stdlib/promise/polyfill/lib/resolve.js | 110 +++++++++ 9 files changed, 670 insertions(+) create mode 100644 lib/node_modules/@stdlib/promise/polyfill/lib/fulfill.js create mode 100644 lib/node_modules/@stdlib/promise/polyfill/lib/fulfilled.js create mode 100644 lib/node_modules/@stdlib/promise/polyfill/lib/handle.js create mode 100644 lib/node_modules/@stdlib/promise/polyfill/lib/index.js create mode 100644 lib/node_modules/@stdlib/promise/polyfill/lib/main.js create mode 100644 lib/node_modules/@stdlib/promise/polyfill/lib/pending.js create mode 100644 lib/node_modules/@stdlib/promise/polyfill/lib/reject.js create mode 100644 lib/node_modules/@stdlib/promise/polyfill/lib/rejected.js create mode 100644 lib/node_modules/@stdlib/promise/polyfill/lib/resolve.js diff --git a/lib/node_modules/@stdlib/promise/polyfill/lib/fulfill.js b/lib/node_modules/@stdlib/promise/polyfill/lib/fulfill.js new file mode 100644 index 000000000000..1ade794d1ba7 --- /dev/null +++ b/lib/node_modules/@stdlib/promise/polyfill/lib/fulfill.js @@ -0,0 +1,52 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-underscore-dangle */ + +'use strict'; + +// MODULES // + +var FULFILLED = require( './fulfilled.js' ); +var handle = require( './handle.js' ); +var PENDING = require( './pending.js' ); + + +// MAIN // + +/** +* Fulfills a promise. +* +* @private +* @param {Object} promise - promise +* @param {*} value - fulfillment value +* @returns {void} +*/ +function fulfill( promise, value ) { + if ( promise._state !== PENDING ) { + return; + } + promise._state = FULFILLED; + promise._value = value; + handle( promise ); +} + + +// EXPORTS // + +module.exports = fulfill; diff --git a/lib/node_modules/@stdlib/promise/polyfill/lib/fulfilled.js b/lib/node_modules/@stdlib/promise/polyfill/lib/fulfilled.js new file mode 100644 index 000000000000..4ad0ef61fd5c --- /dev/null +++ b/lib/node_modules/@stdlib/promise/polyfill/lib/fulfilled.js @@ -0,0 +1,35 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MAIN // + +/** +* Fulfilled state. +* +* @private +* @constant +* @type {number} +*/ +var FULFILLED = 1; + + +// EXPORTS // + +module.exports = FULFILLED; diff --git a/lib/node_modules/@stdlib/promise/polyfill/lib/handle.js b/lib/node_modules/@stdlib/promise/polyfill/lib/handle.js new file mode 100644 index 000000000000..b48d24cff71f --- /dev/null +++ b/lib/node_modules/@stdlib/promise/polyfill/lib/handle.js @@ -0,0 +1,70 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-underscore-dangle */ + +'use strict'; + +// MODULES // + +var nextTick = require( '@stdlib/utils/next-tick' ); +var PENDING = require( './pending.js' ); +var FULFILLED = require( './fulfilled.js' ); + + +// MAIN // + +/** +* Schedules invocation of queued promise handlers. +* +* @private +* @param {Object} promise - promise +*/ +function handle( promise ) { + if ( promise._state === PENDING ) { + return; + } + nextTick( run ); + + /** + * Invokes queued handlers. + * + * @private + */ + function run() { + var handlers; + var handler; + var i; + + handlers = promise._handlers; + promise._handlers = []; + for ( i = 0; i < handlers.length; i++ ) { + handler = handlers[ i ]; + if ( promise._state === FULFILLED ) { + handler.onFulfilled( promise._value ); + } else { + handler.onRejected( promise._value ); + } + } + } +} + + +// EXPORTS // + +module.exports = handle; diff --git a/lib/node_modules/@stdlib/promise/polyfill/lib/index.js b/lib/node_modules/@stdlib/promise/polyfill/lib/index.js new file mode 100644 index 000000000000..458410d20fcf --- /dev/null +++ b/lib/node_modules/@stdlib/promise/polyfill/lib/index.js @@ -0,0 +1,50 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Promise constructor polyfill. +* +* @module @stdlib/promise/polyfill +* +* @example +* var Promise = require( '@stdlib/promise/polyfill' ); +* +* function executor( resolve, reject ) { +* resolve( 'beep' ); +* } +* +* var p = new Promise( executor ); +* +* p.then( onResolve ); +* +* function onResolve( value ) { +* console.log( value ); +* // => 'beep' +* } +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/promise/polyfill/lib/main.js b/lib/node_modules/@stdlib/promise/polyfill/lib/main.js new file mode 100644 index 000000000000..3a275acc96c1 --- /dev/null +++ b/lib/node_modules/@stdlib/promise/polyfill/lib/main.js @@ -0,0 +1,231 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-restricted-syntax, no-invalid-this, no-underscore-dangle */ + +'use strict'; + +// MODULES // + +var isFunction = require( '@stdlib/assert/is-function' ); +var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); +var format = require( '@stdlib/string/format' ); +var handle = require( './handle.js' ); +var resolve = require( './resolve.js' ); +var reject = require( './reject.js' ); +var PENDING = require( './pending.js' ); + + +// MAIN // + +/** +* Promise constructor polyfill. +* +* @constructor +* @param {Function} executor - function invoked immediately with `resolve` and `reject` callbacks +* @throws {TypeError} must invoke with the `new` keyword +* @throws {TypeError} must provide an executor function +* @returns {Promise} promise instance +* +* @example +* function executor( resolve ) { +* resolve( 'beep' ); +* } +* +* var p = new Promise( executor ); +* +* p.then( onResolve ); +* +* function onResolve( value ) { +* console.log( value ); +* // => 'beep' +* } +*/ +function Promise( executor ) { + var self; + if ( !( this instanceof Promise ) ) { + throw new TypeError( format( 'invalid invocation. Constructor must be called with the `new` operator.' ) ); + } + if ( !isFunction( executor ) ) { + throw new TypeError( format( 'invalid argument. Must provide a function. Value: `%s`.', executor ) ); + } + self = this; + this._state = PENDING; + this._value = void 0; + this._handlers = []; + try { + executor( onResolve, onReject ); + } catch ( err ) { + onReject( err ); + } + return this; + + /** + * Resolves a promise. + * + * @private + * @param {*} value - resolution value + */ + function onResolve( value ) { + resolve( self, value ); + } + + /** + * Rejects a promise. + * + * @private + * @param {*} reason - rejection reason + */ + function onReject( reason ) { + if ( self._state !== PENDING ) { + return; + } + reject( self, reason ); + } +} + +/** +* Attaches fulfillment and rejection handlers to a promise. +* +* @name then +* @memberof Promise.prototype +* @type {Function} +* @param {Function} [onFulfilled] - fulfillment handler +* @param {Function} [onRejected] - rejection handler +* @returns {Promise} new promise +*/ +setReadOnly( Promise.prototype, 'then', function then( onFulfilled, onRejected ) { + var self = this; + return new Promise( executor ); + + /** + * Promise executor. + * + * @private + * @param {Function} resolveValue - resolve callback + * @param {Function} rejectValue - reject callback + */ + function executor( resolveValue, rejectValue ) { + self._handlers.push({ + 'onFulfilled': createHandler( onFulfilled, true ), + 'onRejected': createHandler( onRejected, false ) + }); + handle( self ); + + /** + * Returns a handler wrapper. + * + * @private + * @param {Function} cb - callback + * @param {boolean} isFulfilled - boolean indicating whether the callback is a fulfillment handler + * @returns {Function} handler + */ + function createHandler( cb, isFulfilled ) { + return wrapper; + + /** + * Invokes a settlement handler. + * + * @private + * @param {*} value - settlement value + * @returns {void} + */ + function wrapper( value ) { + if ( !isFunction( cb ) ) { + if ( isFulfilled ) { + return resolveValue( value ); + } + return rejectValue( value ); + } + try { + return resolveValue( cb( value ) ); + } catch ( err ) { + return rejectValue( err ); + } + } + } + } +}); + +/** +* Attaches a rejection handler to a promise. +* +* @name catch +* @memberof Promise.prototype +* @type {Function} +* @param {Function} onRejected - rejection handler +* @returns {Promise} new promise +*/ +setReadOnly( Promise.prototype, 'catch', function promiseCatch( onRejected ) { + return this.then( null, onRejected ); +}); + +/** +* Returns a promise resolved with a provided value. +* +* @name resolve +* @memberof Promise +* @type {Function} +* @param {*} value - resolution value +* @returns {Promise} promise +*/ +setReadOnly( Promise, 'resolve', function promiseResolve( value ) { + if ( value instanceof Promise ) { + return value; + } + return new Promise( executor ); + + /** + * Promise executor. + * + * @private + * @param {Function} resolveValue - resolve callback + */ + function executor( resolveValue ) { + resolveValue( value ); + } +}); + +/** +* Returns a promise rejected with a provided reason. +* +* @name reject +* @memberof Promise +* @type {Function} +* @param {*} reason - rejection reason +* @returns {Promise} promise +*/ +setReadOnly( Promise, 'reject', function promiseReject( reason ) { + return new Promise( executor ); + + /** + * Promise executor. + * + * @private + * @param {Function} resolveValue - resolve callback + * @param {Function} rejectValue - reject callback + */ + function executor( resolveValue, rejectValue ) { + rejectValue( reason ); + } +}); + + +// EXPORTS // + +module.exports = Promise; diff --git a/lib/node_modules/@stdlib/promise/polyfill/lib/pending.js b/lib/node_modules/@stdlib/promise/polyfill/lib/pending.js new file mode 100644 index 000000000000..5c09cf8f9f50 --- /dev/null +++ b/lib/node_modules/@stdlib/promise/polyfill/lib/pending.js @@ -0,0 +1,35 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MAIN // + +/** +* Pending state. +* +* @private +* @constant +* @type {number} +*/ +var PENDING = 0; + + +// EXPORTS // + +module.exports = PENDING; diff --git a/lib/node_modules/@stdlib/promise/polyfill/lib/reject.js b/lib/node_modules/@stdlib/promise/polyfill/lib/reject.js new file mode 100644 index 000000000000..1bc4050c353b --- /dev/null +++ b/lib/node_modules/@stdlib/promise/polyfill/lib/reject.js @@ -0,0 +1,52 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-underscore-dangle */ + +'use strict'; + +// MODULES // + +var handle = require( './handle.js' ); +var PENDING = require( './pending.js' ); +var REJECTED = require( './rejected.js' ); + + +// MAIN // + +/** +* Rejects a promise. +* +* @private +* @param {Object} promise - promise +* @param {*} reason - rejection reason +* @returns {void} +*/ +function reject( promise, reason ) { + if ( promise._state !== PENDING ) { + return; + } + promise._state = REJECTED; + promise._value = reason; + handle( promise ); +} + + +// EXPORTS // + +module.exports = reject; diff --git a/lib/node_modules/@stdlib/promise/polyfill/lib/rejected.js b/lib/node_modules/@stdlib/promise/polyfill/lib/rejected.js new file mode 100644 index 000000000000..505e38a969cd --- /dev/null +++ b/lib/node_modules/@stdlib/promise/polyfill/lib/rejected.js @@ -0,0 +1,35 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MAIN // + +/** +* Rejected state. +* +* @private +* @constant +* @type {number} +*/ +var REJECTED = 2; + + +// EXPORTS // + +module.exports = REJECTED; diff --git a/lib/node_modules/@stdlib/promise/polyfill/lib/resolve.js b/lib/node_modules/@stdlib/promise/polyfill/lib/resolve.js new file mode 100644 index 000000000000..8544942e4974 --- /dev/null +++ b/lib/node_modules/@stdlib/promise/polyfill/lib/resolve.js @@ -0,0 +1,110 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-underscore-dangle */ + +'use strict'; + +// MODULES // + +var isFunction = require( '@stdlib/assert/is-function' ); +var format = require( '@stdlib/string/format' ); +var fulfill = require( './fulfill.js' ); +var reject = require( './reject.js' ); +var PENDING = require( './pending.js' ); + + +// MAIN // + +/** +* Resolves a promise according to the Promises/A+ specification. +* +* @private +* @param {Object} promise - promise +* @param {*} value - resolution value +* @returns {void} +*/ +function resolve( promise, value ) { + var called; + var then; + + if ( promise._state !== PENDING ) { + return; + } + if ( value === promise ) { + reject( promise, new TypeError( format( 'invalid value. A promise cannot be resolved with itself. Value: `%s`.', value ) ) ); + return; + } + if ( + value !== null && + ( typeof value === 'object' || typeof value === 'function' ) + ) { + try { + then = value.then; + } catch ( err ) { + reject( promise, err ); + return; + } + if ( isFunction( then ) ) { + called = false; + try { + then.call( value, onFulfilled, onRejected ); + } catch ( err ) { + if ( !called ) { + called = true; + reject( promise, err ); + } + } + return; + } + } + fulfill( promise, value ); + + /** + * Callback invoked upon thenable fulfillment. + * + * @private + * @param {*} v - fulfillment value + */ + function onFulfilled( v ) { + if ( called ) { + return; + } + called = true; + resolve( promise, v ); + } + + /** + * Callback invoked upon thenable rejection. + * + * @private + * @param {*} r - rejection reason + */ + function onRejected( r ) { + if ( called ) { + return; + } + called = true; + reject( promise, r ); + } +} + + +// EXPORTS // + +module.exports = resolve; From 3a8364956609ac868594b3afa85ef8578a9c941c Mon Sep 17 00:00:00 2001 From: 0PrashantYadav0 Date: Mon, 10 Aug 2026 19:34:05 +0530 Subject: [PATCH 2/9] feat: add examples, benchmark and tests --- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown_pkg_readmes status: na - task: lint_markdown_docs status: na - task: lint_markdown status: na - task: lint_package_json status: na - task: lint_repl_help status: passed - task: lint_javascript_src status: na - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed --- --- .../promise/polyfill/benchmark/benchmark.js | 51 ++++ .../@stdlib/promise/polyfill/docs/repl.txt | 23 ++ .../promise/polyfill/docs/types/index.d.ts | 43 +++ .../promise/polyfill/docs/types/test.ts | 43 +++ .../promise/polyfill/examples/index.js | 58 ++++ .../@stdlib/promise/polyfill/test/test.js | 271 ++++++++++++++++++ 6 files changed, 489 insertions(+) create mode 100644 lib/node_modules/@stdlib/promise/polyfill/benchmark/benchmark.js create mode 100644 lib/node_modules/@stdlib/promise/polyfill/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/promise/polyfill/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/promise/polyfill/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/promise/polyfill/examples/index.js create mode 100644 lib/node_modules/@stdlib/promise/polyfill/test/test.js diff --git a/lib/node_modules/@stdlib/promise/polyfill/benchmark/benchmark.js b/lib/node_modules/@stdlib/promise/polyfill/benchmark/benchmark.js new file mode 100644 index 000000000000..5f9274fac288 --- /dev/null +++ b/lib/node_modules/@stdlib/promise/polyfill/benchmark/benchmark.js @@ -0,0 +1,51 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var pkg = require( './../package.json' ).name; +var Promise = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var p; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + p = new Promise( executor ); + if ( typeof p.then !== 'function' ) { + b.fail( 'should return a promise' ); + } + } + b.toc(); + if ( typeof p.then !== 'function' ) { + b.fail( 'should return a promise' ); + } + b.pass( 'benchmark finished' ); + b.end(); + + function executor( resolve ) { + resolve( i ); + } +}); diff --git a/lib/node_modules/@stdlib/promise/polyfill/docs/repl.txt b/lib/node_modules/@stdlib/promise/polyfill/docs/repl.txt new file mode 100644 index 000000000000..3f3522810945 --- /dev/null +++ b/lib/node_modules/@stdlib/promise/polyfill/docs/repl.txt @@ -0,0 +1,23 @@ + +{{alias}}( executor ) + Promise constructor polyfill. + + Parameters + ---------- + executor: Function + Function invoked immediately with `resolve` and `reject` callbacks. + + Returns + ------- + out: Promise + Promise instance. + + Examples + -------- + > function executor( resolve ) { resolve( 'beep' ); }; + > var p = new {{alias}}( executor ) + + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/promise/polyfill/docs/types/index.d.ts b/lib/node_modules/@stdlib/promise/polyfill/docs/types/index.d.ts new file mode 100644 index 000000000000..26bc0b243e5d --- /dev/null +++ b/lib/node_modules/@stdlib/promise/polyfill/docs/types/index.d.ts @@ -0,0 +1,43 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 4.1 + +// EXPORTS // + +/** +* Promise constructor polyfill. +* +* @param executor - function invoked immediately with `resolve` and `reject` callbacks +* @returns promise +* +* @example +* function executor( resolve, reject ) { +* resolve( 'beep' ); +* } +* +* var p = new Promise( executor ); +* +* p.then( onResolve ); +* +* function onResolve( value ) { +* console.log( value ); +* // => 'beep' +* } +*/ +export = Promise; diff --git a/lib/node_modules/@stdlib/promise/polyfill/docs/types/test.ts b/lib/node_modules/@stdlib/promise/polyfill/docs/types/test.ts new file mode 100644 index 000000000000..18a21ad73306 --- /dev/null +++ b/lib/node_modules/@stdlib/promise/polyfill/docs/types/test.ts @@ -0,0 +1,43 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable @typescript-eslint/no-unused-expressions, @typescript-eslint/no-floating-promises */ + +import Promise = require( './index' ); + +/** +* Promise executor. +* +* @param resolve - resolve callback +*/ +function executor( resolve: ( value: number ) => void ): void { + resolve( 1 ); +} + + +// TESTS // + +// The function returns a promise... +{ + new Promise( executor ); // $ExpectType Promise +} + +// The constructor function has to be invoked with `new`... +{ + Promise( executor ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/promise/polyfill/examples/index.js b/lib/node_modules/@stdlib/promise/polyfill/examples/index.js new file mode 100644 index 000000000000..510c671997f3 --- /dev/null +++ b/lib/node_modules/@stdlib/promise/polyfill/examples/index.js @@ -0,0 +1,58 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var Promise = require( './../lib' ); + +function main() { + var p = new Promise( executor ); + p.then( onResolve, onReject ); +} + +/** +* Promise executor. +* +* @private +* @param {Function} resolve - callback to invoke upon fulfilling a promise +*/ +function executor( resolve ) { + resolve( 'beep' ); +} + +/** +* Callback invoked upon promise fulfillment. +* +* @private +* @param {*} value - resolved value +*/ +function onResolve( value ) { + console.log( value ); +} + +/** +* Callback invoked upon promise rejection. +* +* @private +* @param {Error} error - error +*/ +function onReject( error ) { + console.error( error.message ); +} + +main(); diff --git a/lib/node_modules/@stdlib/promise/polyfill/test/test.js b/lib/node_modules/@stdlib/promise/polyfill/test/test.js new file mode 100644 index 000000000000..c4749df06926 --- /dev/null +++ b/lib/node_modules/@stdlib/promise/polyfill/test/test.js @@ -0,0 +1,271 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Promise = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof Promise, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function throws an error if not invoked with the `new` keyword', function test( t ) { + t.throws( foo, TypeError, 'throws an error' ); + t.end(); + + function foo() { + Promise( executor ); // eslint-disable-line new-cap + } + + function executor( resolve ) { + resolve( 1 ); + } +}); + +tape( 'the function throws an error if not provided a function', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided '+values[ i ] ); + } + t.end(); + + function badValue( value ) { + return wrapper; + + function wrapper() { + var p = new Promise( value ); // eslint-disable-line no-unused-vars + } + } +}); + +tape( 'the function is a constructor which returns a promise', function test( t ) { + var p = new Promise( executor ); + t.strictEqual( typeof p.then, 'function', 'returns expected value' ); + t.strictEqual( typeof p.catch, 'function', 'returns expected value' ); + t.end(); + + function executor( resolve ) { + resolve( 1 ); + } +}); + +tape( 'the constructor returns a promise which fulfills with a provided value', function test( t ) { + var p = new Promise( executor ); + p.then( onFulfill, onReject ); + + function executor( resolve ) { + resolve( 1 ); + } + + function onFulfill( value ) { + t.strictEqual( value, 1, 'returns expected value' ); + t.end(); + } + + function onReject( error ) { + t.fail( error.message ); + t.end(); + } +}); + +tape( 'the constructor returns a promise which rejects with a provided reason', function test( t ) { + var p = new Promise( executor ); + p.then( onFulfill, onReject ); + + function executor( resolve, reject ) { + reject( new Error( 'beep' ) ); + } + + function onFulfill() { + t.fail( 'should not fulfill' ); + t.end(); + } + + function onReject( error ) { + t.strictEqual( error.message, 'beep', 'returns expected value' ); + t.end(); + } +}); + +tape( 'the constructor has a `resolve` method which returns a fulfilled promise', function test( t ) { + var p = Promise.resolve( 2 ); + p.then( onFulfill, onReject ); + + function onFulfill( value ) { + t.strictEqual( value, 2, 'returns expected value' ); + t.end(); + } + + function onReject( error ) { + t.fail( error.message ); + t.end(); + } +}); + +tape( 'the constructor has a `reject` method which returns a rejected promise', function test( t ) { + var p = Promise.reject( new Error( 'boop' ) ); + p.then( onFulfill, onReject ); + + function onFulfill() { + t.fail( 'should not fulfill' ); + t.end(); + } + + function onReject( error ) { + t.strictEqual( error.message, 'boop', 'returns expected value' ); + t.end(); + } +}); + +tape( 'the `catch` method attaches a rejection handler', function test( t ) { + var p = Promise.reject( new Error( 'bop' ) ); + p.catch( onReject ); + + function onReject( error ) { + t.strictEqual( error.message, 'bop', 'returns expected value' ); + t.end(); + } +}); + +tape( 'if an executor throws an error, the promise is rejected', function test( t ) { + var p = new Promise( executor ); + p.then( onFulfill, onReject ); + + function executor() { + throw new Error( 'oops' ); + } + + function onFulfill() { + t.fail( 'should not fulfill' ); + t.end(); + } + + function onReject( error ) { + t.strictEqual( error.message, 'oops', 'returns expected value' ); + t.end(); + } +}); + +tape( 'the `then` method supports chaining', function test( t ) { + var p = Promise.resolve( 2 ); + p.then( onFulfill ).then( onChained, onReject ); + + function onFulfill( value ) { + return value * 3; + } + + function onChained( value ) { + t.strictEqual( value, 6, 'returns expected value' ); + t.end(); + } + + function onReject( error ) { + t.fail( error.message ); + t.end(); + } +}); + +tape( 'resolving with a thenable adopts the thenable state', function test( t ) { + var thenable; + var p; + + thenable = { + 'then': then + }; + p = Promise.resolve( thenable ); + p.then( onFulfill, onReject ); + + function then( resolve ) { + resolve( 'adopted' ); + } + + function onFulfill( value ) { + t.strictEqual( value, 'adopted', 'returns expected value' ); + t.end(); + } + + function onReject( error ) { + t.fail( error.message ); + t.end(); + } +}); + +tape( 'resolving a promise with itself rejects the promise', function test( t ) { + var resolve; + var p; + + p = new Promise( executor ); + p.then( onFulfill, onReject ); + resolve( p ); + + function executor( res ) { + resolve = res; + } + + function onFulfill() { + t.fail( 'should not fulfill' ); + t.end(); + } + + function onReject( error ) { + t.strictEqual( error instanceof TypeError, true, 'returns expected value' ); + t.end(); + } +}); + +tape( 'subsequent resolve and reject calls are ignored after settlement', function test( t ) { + var p = new Promise( executor ); + p.then( onFulfill, onReject ); + + function executor( resolve, reject ) { + resolve( 1 ); + resolve( 2 ); + reject( new Error( 'beep' ) ); + } + + function onFulfill( value ) { + t.strictEqual( value, 1, 'returns expected value' ); + t.end(); + } + + function onReject( error ) { + t.fail( error.message ); + t.end(); + } +}); From ed39e419b8d8ab557a326cebe485a14ed8164b46 Mon Sep 17 00:00:00 2001 From: 0PrashantYadav0 Date: Mon, 10 Aug 2026 19:36:06 +0530 Subject: [PATCH 3/9] feat: add readme and package.json file --- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown_pkg_readmes status: passed - task: lint_markdown_docs status: na - task: lint_markdown status: na - task: lint_package_json status: passed - task: lint_repl_help status: na - task: lint_javascript_src status: na - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: na - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: na - task: lint_license_headers status: passed --- --- .../@stdlib/promise/polyfill/README.md | 134 ++++++++++++++++++ .../@stdlib/promise/polyfill/package.json | 62 ++++++++ 2 files changed, 196 insertions(+) create mode 100644 lib/node_modules/@stdlib/promise/polyfill/README.md create mode 100644 lib/node_modules/@stdlib/promise/polyfill/package.json diff --git a/lib/node_modules/@stdlib/promise/polyfill/README.md b/lib/node_modules/@stdlib/promise/polyfill/README.md new file mode 100644 index 000000000000..daa67dbed924 --- /dev/null +++ b/lib/node_modules/@stdlib/promise/polyfill/README.md @@ -0,0 +1,134 @@ + + +# Promise Polyfill + +> [Promise][mdn-promise] constructor polyfill. + + + +
+ +
+ + + + + +
+ +## Usage + +```javascript +var Promise = require( '@stdlib/promise/polyfill' ); +``` + +#### Promise( executor ) + +Returns a new [Promise][mdn-promise]. + +```javascript +function executor( resolve, reject ) { + resolve( 'beep' ); +} + +var p = new Promise( executor ); + +p.then( onResolve ); + +function onResolve( value ) { + console.log( value ); + // => 'beep' +} +``` + +The `executor` function is invoked immediately with two arguments: + +- **resolve**: callback to invoke upon fulfilling a promise. +- **reject**: callback to invoke upon rejecting a promise. + +
+ + + + + +
+ +## Notes + +- This package provides a constructor-focused [Promise][mdn-promise] polyfill supporting `then`, `catch`, `Promise.resolve`, and `Promise.reject`. It is intended as a fallback for environments without native Promise support (for example, via [`@stdlib/promise/ctor`][@stdlib/promise/ctor]). +- Asynchronous settlement is scheduled with [`@stdlib/utils/next-tick`][@stdlib/utils/next-tick], which depends on `process.nextTick`. +- Rejected promises without an attached rejection handler do not emit an `unhandledRejection` event. + +
+ + + + + +
+ +## Examples + +```javascript +var Promise = require( '@stdlib/promise/polyfill' ); + +function executor( resolve ) { + setTimeout( onTimeout, 0 ); + function onTimeout() { + resolve( 'beep' ); + } +} + +var p = new Promise( executor ); + +p.then( onResolve ); + +function onResolve( value ) { + console.log( value ); +} +``` + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/promise/polyfill/package.json b/lib/node_modules/@stdlib/promise/polyfill/package.json new file mode 100644 index 000000000000..5b2d7fbadadf --- /dev/null +++ b/lib/node_modules/@stdlib/promise/polyfill/package.json @@ -0,0 +1,62 @@ +{ + "name": "@stdlib/promise/polyfill", + "version": "0.0.0", + "description": "Promise constructor polyfill.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdtypes", + "types", + "promise", + "promises", + "polyfill", + "ponyfill", + "es2015", + "es6" + ] +} From 8589a704e6763469e0693364df0d8495d2d95432 Mon Sep 17 00:00:00 2001 From: 0PrashantYadav0 Date: Tue, 11 Aug 2026 11:29:28 +0530 Subject: [PATCH 4/9] feat: add @stdlib/promise/ctor --- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown_pkg_readmes status: na - task: lint_markdown_docs status: na - task: lint_markdown status: na - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: na - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: na - task: lint_license_headers status: passed --- --- .../@stdlib/promise/ctor/lib/index.js | 62 +++++ .../@stdlib/promise/ctor/lib/main.js | 40 +++ .../promise/ctor/lib/polyfill/fulfill.js | 52 ++++ .../promise/ctor/lib/polyfill/fulfilled.js | 35 +++ .../promise/ctor/lib/polyfill/handle.js | 70 ++++++ .../promise/ctor/lib/polyfill/index.js | 231 ++++++++++++++++++ .../promise/ctor/lib/polyfill/pending.js | 35 +++ .../promise/ctor/lib/polyfill/reject.js | 52 ++++ .../promise/ctor/lib/polyfill/rejected.js | 35 +++ .../promise/ctor/lib/polyfill/resolve.js | 110 +++++++++ .../@stdlib/promise/ctor/lib/promise.js | 28 +++ 11 files changed, 750 insertions(+) create mode 100644 lib/node_modules/@stdlib/promise/ctor/lib/index.js create mode 100644 lib/node_modules/@stdlib/promise/ctor/lib/main.js create mode 100644 lib/node_modules/@stdlib/promise/ctor/lib/polyfill/fulfill.js create mode 100644 lib/node_modules/@stdlib/promise/ctor/lib/polyfill/fulfilled.js create mode 100644 lib/node_modules/@stdlib/promise/ctor/lib/polyfill/handle.js create mode 100644 lib/node_modules/@stdlib/promise/ctor/lib/polyfill/index.js create mode 100644 lib/node_modules/@stdlib/promise/ctor/lib/polyfill/pending.js create mode 100644 lib/node_modules/@stdlib/promise/ctor/lib/polyfill/reject.js create mode 100644 lib/node_modules/@stdlib/promise/ctor/lib/polyfill/rejected.js create mode 100644 lib/node_modules/@stdlib/promise/ctor/lib/polyfill/resolve.js create mode 100644 lib/node_modules/@stdlib/promise/ctor/lib/promise.js diff --git a/lib/node_modules/@stdlib/promise/ctor/lib/index.js b/lib/node_modules/@stdlib/promise/ctor/lib/index.js new file mode 100644 index 000000000000..d633d9241a3f --- /dev/null +++ b/lib/node_modules/@stdlib/promise/ctor/lib/index.js @@ -0,0 +1,62 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Promise constructor. +* +* @module @stdlib/promise/ctor +* +* @example +* var Promise = require( '@stdlib/promise/ctor' ); +* +* function executor( resolve, reject ) { +* resolve( 'beep' ); +* } +* +* var p = new Promise( executor ); +* +* p.then( onResolve ); +* +* function onResolve( value ) { +* console.log( value ); +* // => 'beep' +* } +*/ + +// MODULES // + +var hasPromiseSupport = require( '@stdlib/assert/has-promise-support' ); +var builtin = require( './main.js' ); +var polyfill = require( './polyfill' ); + + +// MAIN // + +var main; +if ( hasPromiseSupport() ) { + main = builtin; +} else { + main = polyfill; +} + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/promise/ctor/lib/main.js b/lib/node_modules/@stdlib/promise/ctor/lib/main.js new file mode 100644 index 000000000000..2c686a11994b --- /dev/null +++ b/lib/node_modules/@stdlib/promise/ctor/lib/main.js @@ -0,0 +1,40 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var PromiseCtor = require( './promise.js' ); + + +// MAIN // + +/** +* Promise constructor. +* +* @name Promise +* @constructor +* @type {Function} +*/ +var main = PromiseCtor; + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/promise/ctor/lib/polyfill/fulfill.js b/lib/node_modules/@stdlib/promise/ctor/lib/polyfill/fulfill.js new file mode 100644 index 000000000000..1ade794d1ba7 --- /dev/null +++ b/lib/node_modules/@stdlib/promise/ctor/lib/polyfill/fulfill.js @@ -0,0 +1,52 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-underscore-dangle */ + +'use strict'; + +// MODULES // + +var FULFILLED = require( './fulfilled.js' ); +var handle = require( './handle.js' ); +var PENDING = require( './pending.js' ); + + +// MAIN // + +/** +* Fulfills a promise. +* +* @private +* @param {Object} promise - promise +* @param {*} value - fulfillment value +* @returns {void} +*/ +function fulfill( promise, value ) { + if ( promise._state !== PENDING ) { + return; + } + promise._state = FULFILLED; + promise._value = value; + handle( promise ); +} + + +// EXPORTS // + +module.exports = fulfill; diff --git a/lib/node_modules/@stdlib/promise/ctor/lib/polyfill/fulfilled.js b/lib/node_modules/@stdlib/promise/ctor/lib/polyfill/fulfilled.js new file mode 100644 index 000000000000..4ad0ef61fd5c --- /dev/null +++ b/lib/node_modules/@stdlib/promise/ctor/lib/polyfill/fulfilled.js @@ -0,0 +1,35 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MAIN // + +/** +* Fulfilled state. +* +* @private +* @constant +* @type {number} +*/ +var FULFILLED = 1; + + +// EXPORTS // + +module.exports = FULFILLED; diff --git a/lib/node_modules/@stdlib/promise/ctor/lib/polyfill/handle.js b/lib/node_modules/@stdlib/promise/ctor/lib/polyfill/handle.js new file mode 100644 index 000000000000..b48d24cff71f --- /dev/null +++ b/lib/node_modules/@stdlib/promise/ctor/lib/polyfill/handle.js @@ -0,0 +1,70 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-underscore-dangle */ + +'use strict'; + +// MODULES // + +var nextTick = require( '@stdlib/utils/next-tick' ); +var PENDING = require( './pending.js' ); +var FULFILLED = require( './fulfilled.js' ); + + +// MAIN // + +/** +* Schedules invocation of queued promise handlers. +* +* @private +* @param {Object} promise - promise +*/ +function handle( promise ) { + if ( promise._state === PENDING ) { + return; + } + nextTick( run ); + + /** + * Invokes queued handlers. + * + * @private + */ + function run() { + var handlers; + var handler; + var i; + + handlers = promise._handlers; + promise._handlers = []; + for ( i = 0; i < handlers.length; i++ ) { + handler = handlers[ i ]; + if ( promise._state === FULFILLED ) { + handler.onFulfilled( promise._value ); + } else { + handler.onRejected( promise._value ); + } + } + } +} + + +// EXPORTS // + +module.exports = handle; diff --git a/lib/node_modules/@stdlib/promise/ctor/lib/polyfill/index.js b/lib/node_modules/@stdlib/promise/ctor/lib/polyfill/index.js new file mode 100644 index 000000000000..3a275acc96c1 --- /dev/null +++ b/lib/node_modules/@stdlib/promise/ctor/lib/polyfill/index.js @@ -0,0 +1,231 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-restricted-syntax, no-invalid-this, no-underscore-dangle */ + +'use strict'; + +// MODULES // + +var isFunction = require( '@stdlib/assert/is-function' ); +var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); +var format = require( '@stdlib/string/format' ); +var handle = require( './handle.js' ); +var resolve = require( './resolve.js' ); +var reject = require( './reject.js' ); +var PENDING = require( './pending.js' ); + + +// MAIN // + +/** +* Promise constructor polyfill. +* +* @constructor +* @param {Function} executor - function invoked immediately with `resolve` and `reject` callbacks +* @throws {TypeError} must invoke with the `new` keyword +* @throws {TypeError} must provide an executor function +* @returns {Promise} promise instance +* +* @example +* function executor( resolve ) { +* resolve( 'beep' ); +* } +* +* var p = new Promise( executor ); +* +* p.then( onResolve ); +* +* function onResolve( value ) { +* console.log( value ); +* // => 'beep' +* } +*/ +function Promise( executor ) { + var self; + if ( !( this instanceof Promise ) ) { + throw new TypeError( format( 'invalid invocation. Constructor must be called with the `new` operator.' ) ); + } + if ( !isFunction( executor ) ) { + throw new TypeError( format( 'invalid argument. Must provide a function. Value: `%s`.', executor ) ); + } + self = this; + this._state = PENDING; + this._value = void 0; + this._handlers = []; + try { + executor( onResolve, onReject ); + } catch ( err ) { + onReject( err ); + } + return this; + + /** + * Resolves a promise. + * + * @private + * @param {*} value - resolution value + */ + function onResolve( value ) { + resolve( self, value ); + } + + /** + * Rejects a promise. + * + * @private + * @param {*} reason - rejection reason + */ + function onReject( reason ) { + if ( self._state !== PENDING ) { + return; + } + reject( self, reason ); + } +} + +/** +* Attaches fulfillment and rejection handlers to a promise. +* +* @name then +* @memberof Promise.prototype +* @type {Function} +* @param {Function} [onFulfilled] - fulfillment handler +* @param {Function} [onRejected] - rejection handler +* @returns {Promise} new promise +*/ +setReadOnly( Promise.prototype, 'then', function then( onFulfilled, onRejected ) { + var self = this; + return new Promise( executor ); + + /** + * Promise executor. + * + * @private + * @param {Function} resolveValue - resolve callback + * @param {Function} rejectValue - reject callback + */ + function executor( resolveValue, rejectValue ) { + self._handlers.push({ + 'onFulfilled': createHandler( onFulfilled, true ), + 'onRejected': createHandler( onRejected, false ) + }); + handle( self ); + + /** + * Returns a handler wrapper. + * + * @private + * @param {Function} cb - callback + * @param {boolean} isFulfilled - boolean indicating whether the callback is a fulfillment handler + * @returns {Function} handler + */ + function createHandler( cb, isFulfilled ) { + return wrapper; + + /** + * Invokes a settlement handler. + * + * @private + * @param {*} value - settlement value + * @returns {void} + */ + function wrapper( value ) { + if ( !isFunction( cb ) ) { + if ( isFulfilled ) { + return resolveValue( value ); + } + return rejectValue( value ); + } + try { + return resolveValue( cb( value ) ); + } catch ( err ) { + return rejectValue( err ); + } + } + } + } +}); + +/** +* Attaches a rejection handler to a promise. +* +* @name catch +* @memberof Promise.prototype +* @type {Function} +* @param {Function} onRejected - rejection handler +* @returns {Promise} new promise +*/ +setReadOnly( Promise.prototype, 'catch', function promiseCatch( onRejected ) { + return this.then( null, onRejected ); +}); + +/** +* Returns a promise resolved with a provided value. +* +* @name resolve +* @memberof Promise +* @type {Function} +* @param {*} value - resolution value +* @returns {Promise} promise +*/ +setReadOnly( Promise, 'resolve', function promiseResolve( value ) { + if ( value instanceof Promise ) { + return value; + } + return new Promise( executor ); + + /** + * Promise executor. + * + * @private + * @param {Function} resolveValue - resolve callback + */ + function executor( resolveValue ) { + resolveValue( value ); + } +}); + +/** +* Returns a promise rejected with a provided reason. +* +* @name reject +* @memberof Promise +* @type {Function} +* @param {*} reason - rejection reason +* @returns {Promise} promise +*/ +setReadOnly( Promise, 'reject', function promiseReject( reason ) { + return new Promise( executor ); + + /** + * Promise executor. + * + * @private + * @param {Function} resolveValue - resolve callback + * @param {Function} rejectValue - reject callback + */ + function executor( resolveValue, rejectValue ) { + rejectValue( reason ); + } +}); + + +// EXPORTS // + +module.exports = Promise; diff --git a/lib/node_modules/@stdlib/promise/ctor/lib/polyfill/pending.js b/lib/node_modules/@stdlib/promise/ctor/lib/polyfill/pending.js new file mode 100644 index 000000000000..5c09cf8f9f50 --- /dev/null +++ b/lib/node_modules/@stdlib/promise/ctor/lib/polyfill/pending.js @@ -0,0 +1,35 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MAIN // + +/** +* Pending state. +* +* @private +* @constant +* @type {number} +*/ +var PENDING = 0; + + +// EXPORTS // + +module.exports = PENDING; diff --git a/lib/node_modules/@stdlib/promise/ctor/lib/polyfill/reject.js b/lib/node_modules/@stdlib/promise/ctor/lib/polyfill/reject.js new file mode 100644 index 000000000000..1bc4050c353b --- /dev/null +++ b/lib/node_modules/@stdlib/promise/ctor/lib/polyfill/reject.js @@ -0,0 +1,52 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-underscore-dangle */ + +'use strict'; + +// MODULES // + +var handle = require( './handle.js' ); +var PENDING = require( './pending.js' ); +var REJECTED = require( './rejected.js' ); + + +// MAIN // + +/** +* Rejects a promise. +* +* @private +* @param {Object} promise - promise +* @param {*} reason - rejection reason +* @returns {void} +*/ +function reject( promise, reason ) { + if ( promise._state !== PENDING ) { + return; + } + promise._state = REJECTED; + promise._value = reason; + handle( promise ); +} + + +// EXPORTS // + +module.exports = reject; diff --git a/lib/node_modules/@stdlib/promise/ctor/lib/polyfill/rejected.js b/lib/node_modules/@stdlib/promise/ctor/lib/polyfill/rejected.js new file mode 100644 index 000000000000..505e38a969cd --- /dev/null +++ b/lib/node_modules/@stdlib/promise/ctor/lib/polyfill/rejected.js @@ -0,0 +1,35 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MAIN // + +/** +* Rejected state. +* +* @private +* @constant +* @type {number} +*/ +var REJECTED = 2; + + +// EXPORTS // + +module.exports = REJECTED; diff --git a/lib/node_modules/@stdlib/promise/ctor/lib/polyfill/resolve.js b/lib/node_modules/@stdlib/promise/ctor/lib/polyfill/resolve.js new file mode 100644 index 000000000000..8544942e4974 --- /dev/null +++ b/lib/node_modules/@stdlib/promise/ctor/lib/polyfill/resolve.js @@ -0,0 +1,110 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-underscore-dangle */ + +'use strict'; + +// MODULES // + +var isFunction = require( '@stdlib/assert/is-function' ); +var format = require( '@stdlib/string/format' ); +var fulfill = require( './fulfill.js' ); +var reject = require( './reject.js' ); +var PENDING = require( './pending.js' ); + + +// MAIN // + +/** +* Resolves a promise according to the Promises/A+ specification. +* +* @private +* @param {Object} promise - promise +* @param {*} value - resolution value +* @returns {void} +*/ +function resolve( promise, value ) { + var called; + var then; + + if ( promise._state !== PENDING ) { + return; + } + if ( value === promise ) { + reject( promise, new TypeError( format( 'invalid value. A promise cannot be resolved with itself. Value: `%s`.', value ) ) ); + return; + } + if ( + value !== null && + ( typeof value === 'object' || typeof value === 'function' ) + ) { + try { + then = value.then; + } catch ( err ) { + reject( promise, err ); + return; + } + if ( isFunction( then ) ) { + called = false; + try { + then.call( value, onFulfilled, onRejected ); + } catch ( err ) { + if ( !called ) { + called = true; + reject( promise, err ); + } + } + return; + } + } + fulfill( promise, value ); + + /** + * Callback invoked upon thenable fulfillment. + * + * @private + * @param {*} v - fulfillment value + */ + function onFulfilled( v ) { + if ( called ) { + return; + } + called = true; + resolve( promise, v ); + } + + /** + * Callback invoked upon thenable rejection. + * + * @private + * @param {*} r - rejection reason + */ + function onRejected( r ) { + if ( called ) { + return; + } + called = true; + reject( promise, r ); + } +} + + +// EXPORTS // + +module.exports = resolve; diff --git a/lib/node_modules/@stdlib/promise/ctor/lib/promise.js b/lib/node_modules/@stdlib/promise/ctor/lib/promise.js new file mode 100644 index 000000000000..7b3070c04403 --- /dev/null +++ b/lib/node_modules/@stdlib/promise/ctor/lib/promise.js @@ -0,0 +1,28 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MAIN // + +var main = ( typeof Promise === 'function' ) ? Promise : null; // eslint-disable-line stdlib/require-globals + + +// EXPORTS // + +module.exports = main; From 6b67a26a4ae115c46ff75536d2376b5a5e5629a2 Mon Sep 17 00:00:00 2001 From: 0PrashantYadav0 Date: Tue, 11 Aug 2026 11:30:30 +0530 Subject: [PATCH 5/9] docs: add documentation and TypeScript definitions --- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown_pkg_readmes status: na - task: lint_markdown_docs status: na - task: lint_markdown status: na - task: lint_package_json status: na - task: lint_repl_help status: passed - task: lint_javascript_src status: na - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: na - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed --- --- .../@stdlib/promise/ctor/docs/repl.txt | 23 ++++++++++ .../promise/ctor/docs/types/index.d.ts | 43 +++++++++++++++++++ .../@stdlib/promise/ctor/docs/types/test.ts | 43 +++++++++++++++++++ 3 files changed, 109 insertions(+) create mode 100644 lib/node_modules/@stdlib/promise/ctor/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/promise/ctor/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/promise/ctor/docs/types/test.ts diff --git a/lib/node_modules/@stdlib/promise/ctor/docs/repl.txt b/lib/node_modules/@stdlib/promise/ctor/docs/repl.txt new file mode 100644 index 000000000000..b42ad0a7a3c2 --- /dev/null +++ b/lib/node_modules/@stdlib/promise/ctor/docs/repl.txt @@ -0,0 +1,23 @@ + +{{alias}}( executor ) + Promise constructor. + + Parameters + ---------- + executor: Function + Function invoked immediately with `resolve` and `reject` callbacks. + + Returns + ------- + out: Promise + Promise instance. + + Examples + -------- + > function executor( resolve ) { resolve( 'beep' ); }; + > var p = new {{alias}}( executor ) + + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/promise/ctor/docs/types/index.d.ts b/lib/node_modules/@stdlib/promise/ctor/docs/types/index.d.ts new file mode 100644 index 000000000000..06e68065fdaf --- /dev/null +++ b/lib/node_modules/@stdlib/promise/ctor/docs/types/index.d.ts @@ -0,0 +1,43 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 4.1 + +// EXPORTS // + +/** +* Promise constructor. +* +* @param executor - function invoked immediately with `resolve` and `reject` callbacks +* @returns promise +* +* @example +* function executor( resolve, reject ) { +* resolve( 'beep' ); +* } +* +* var p = new Promise( executor ); +* +* p.then( onResolve ); +* +* function onResolve( value ) { +* console.log( value ); +* // => 'beep' +* } +*/ +export = Promise; diff --git a/lib/node_modules/@stdlib/promise/ctor/docs/types/test.ts b/lib/node_modules/@stdlib/promise/ctor/docs/types/test.ts new file mode 100644 index 000000000000..18a21ad73306 --- /dev/null +++ b/lib/node_modules/@stdlib/promise/ctor/docs/types/test.ts @@ -0,0 +1,43 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable @typescript-eslint/no-unused-expressions, @typescript-eslint/no-floating-promises */ + +import Promise = require( './index' ); + +/** +* Promise executor. +* +* @param resolve - resolve callback +*/ +function executor( resolve: ( value: number ) => void ): void { + resolve( 1 ); +} + + +// TESTS // + +// The function returns a promise... +{ + new Promise( executor ); // $ExpectType Promise +} + +// The constructor function has to be invoked with `new`... +{ + Promise( executor ); // $ExpectError +} From 2aff2c72f6372b84eb67f231de9d529103d233ef Mon Sep 17 00:00:00 2001 From: 0PrashantYadav0 Date: Tue, 11 Aug 2026 11:31:19 +0530 Subject: [PATCH 6/9] feat: add benchmark, examples, readme and tests --- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown_pkg_readmes status: passed - task: lint_markdown_docs status: na - task: lint_markdown status: na - task: lint_package_json status: passed - task: lint_repl_help status: na - task: lint_javascript_src status: na - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: na - task: lint_license_headers status: passed --- --- .../@stdlib/promise/ctor/README.md | 128 +++++++++ .../promise/ctor/benchmark/benchmark.js | 51 ++++ .../ctor/benchmark/polyfill/benchmark.js | 52 ++++ .../@stdlib/promise/ctor/examples/index.js | 58 ++++ .../@stdlib/promise/ctor/package.json | 64 +++++ .../promise/ctor/test/polyfill/test.js | 271 ++++++++++++++++++ .../@stdlib/promise/ctor/test/test.js | 81 ++++++ .../@stdlib/promise/ctor/test/test.main.js | 70 +++++ 8 files changed, 775 insertions(+) create mode 100644 lib/node_modules/@stdlib/promise/ctor/README.md create mode 100644 lib/node_modules/@stdlib/promise/ctor/benchmark/benchmark.js create mode 100644 lib/node_modules/@stdlib/promise/ctor/benchmark/polyfill/benchmark.js create mode 100644 lib/node_modules/@stdlib/promise/ctor/examples/index.js create mode 100644 lib/node_modules/@stdlib/promise/ctor/package.json create mode 100644 lib/node_modules/@stdlib/promise/ctor/test/polyfill/test.js create mode 100644 lib/node_modules/@stdlib/promise/ctor/test/test.js create mode 100644 lib/node_modules/@stdlib/promise/ctor/test/test.main.js diff --git a/lib/node_modules/@stdlib/promise/ctor/README.md b/lib/node_modules/@stdlib/promise/ctor/README.md new file mode 100644 index 000000000000..7cf51c5b790c --- /dev/null +++ b/lib/node_modules/@stdlib/promise/ctor/README.md @@ -0,0 +1,128 @@ + + +# Promise + +> [Promise][mdn-promise] constructor. + + + +
+ +
+ + + + + +
+ +## Usage + +```javascript +var Promise = require( '@stdlib/promise/ctor' ); +``` + +#### Promise( executor ) + +Returns a new [Promise][mdn-promise]. + +```javascript +function executor( resolve, reject ) { + resolve( 'beep' ); +} + +var p = new Promise( executor ); + +p.then( onResolve ); + +function onResolve( value ) { + console.log( value ); + // => 'beep' +} +``` + +The `executor` function is invoked immediately with two arguments: + +- **resolve**: callback to invoke upon fulfilling a promise. +- **reject**: callback to invoke upon rejecting a promise. + +
+ + + + + +
+ +## Notes + +- In environments lacking native [`Promise`][mdn-promise] support, the package falls back to a Promise constructor polyfill. + +
+ + + + + +
+ +## Examples + +```javascript +var Promise = require( '@stdlib/promise/ctor' ); + +function executor( resolve ) { + setTimeout( onTimeout, 0 ); + function onTimeout() { + resolve( 'beep' ); + } +} + +var p = new Promise( executor ); + +p.then( onResolve ); + +function onResolve( value ) { + console.log( value ); +} +``` + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/promise/ctor/benchmark/benchmark.js b/lib/node_modules/@stdlib/promise/ctor/benchmark/benchmark.js new file mode 100644 index 000000000000..5f9274fac288 --- /dev/null +++ b/lib/node_modules/@stdlib/promise/ctor/benchmark/benchmark.js @@ -0,0 +1,51 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var pkg = require( './../package.json' ).name; +var Promise = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var p; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + p = new Promise( executor ); + if ( typeof p.then !== 'function' ) { + b.fail( 'should return a promise' ); + } + } + b.toc(); + if ( typeof p.then !== 'function' ) { + b.fail( 'should return a promise' ); + } + b.pass( 'benchmark finished' ); + b.end(); + + function executor( resolve ) { + resolve( i ); + } +}); diff --git a/lib/node_modules/@stdlib/promise/ctor/benchmark/polyfill/benchmark.js b/lib/node_modules/@stdlib/promise/ctor/benchmark/polyfill/benchmark.js new file mode 100644 index 000000000000..cab18b471ddb --- /dev/null +++ b/lib/node_modules/@stdlib/promise/ctor/benchmark/polyfill/benchmark.js @@ -0,0 +1,52 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../../package.json' ).name; +var Promise = require( './../../lib/polyfill' ); + + +// MAIN // + +bench( format( '%s::polyfill', pkg ), function benchmark( b ) { + var p; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + p = new Promise( executor ); + if ( typeof p.then !== 'function' ) { + b.fail( 'should return a promise' ); + } + } + b.toc(); + if ( typeof p.then !== 'function' ) { + b.fail( 'should return a promise' ); + } + b.pass( 'benchmark finished' ); + b.end(); + + function executor( resolve ) { + resolve( i ); + } +}); diff --git a/lib/node_modules/@stdlib/promise/ctor/examples/index.js b/lib/node_modules/@stdlib/promise/ctor/examples/index.js new file mode 100644 index 000000000000..510c671997f3 --- /dev/null +++ b/lib/node_modules/@stdlib/promise/ctor/examples/index.js @@ -0,0 +1,58 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var Promise = require( './../lib' ); + +function main() { + var p = new Promise( executor ); + p.then( onResolve, onReject ); +} + +/** +* Promise executor. +* +* @private +* @param {Function} resolve - callback to invoke upon fulfilling a promise +*/ +function executor( resolve ) { + resolve( 'beep' ); +} + +/** +* Callback invoked upon promise fulfillment. +* +* @private +* @param {*} value - resolved value +*/ +function onResolve( value ) { + console.log( value ); +} + +/** +* Callback invoked upon promise rejection. +* +* @private +* @param {Error} error - error +*/ +function onReject( error ) { + console.error( error.message ); +} + +main(); diff --git a/lib/node_modules/@stdlib/promise/ctor/package.json b/lib/node_modules/@stdlib/promise/ctor/package.json new file mode 100644 index 000000000000..24bcdce885c4 --- /dev/null +++ b/lib/node_modules/@stdlib/promise/ctor/package.json @@ -0,0 +1,64 @@ +{ + "name": "@stdlib/promise/ctor", + "version": "0.0.0", + "description": "Promise constructor.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdtypes", + "types", + "promise", + "promises", + "ctor", + "constructor", + "es2015", + "es6", + "polyfill", + "ponyfill" + ] +} diff --git a/lib/node_modules/@stdlib/promise/ctor/test/polyfill/test.js b/lib/node_modules/@stdlib/promise/ctor/test/polyfill/test.js new file mode 100644 index 000000000000..115e95394a39 --- /dev/null +++ b/lib/node_modules/@stdlib/promise/ctor/test/polyfill/test.js @@ -0,0 +1,271 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Promise = require( './../../lib/polyfill' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof Promise, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function throws an error if not invoked with the `new` keyword', function test( t ) { + t.throws( foo, TypeError, 'throws an error' ); + t.end(); + + function foo() { + Promise( executor ); // eslint-disable-line new-cap + } + + function executor( resolve ) { + resolve( 1 ); + } +}); + +tape( 'the function throws an error if not provided a function', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided '+values[ i ] ); + } + t.end(); + + function badValue( value ) { + return wrapper; + + function wrapper() { + var p = new Promise( value ); // eslint-disable-line no-unused-vars + } + } +}); + +tape( 'the function is a constructor which returns a promise', function test( t ) { + var p = new Promise( executor ); + t.strictEqual( typeof p.then, 'function', 'returns expected value' ); + t.strictEqual( typeof p.catch, 'function', 'returns expected value' ); + t.end(); + + function executor( resolve ) { + resolve( 1 ); + } +}); + +tape( 'the constructor returns a promise which fulfills with a provided value', function test( t ) { + var p = new Promise( executor ); + p.then( onFulfill, onReject ); + + function executor( resolve ) { + resolve( 1 ); + } + + function onFulfill( value ) { + t.strictEqual( value, 1, 'returns expected value' ); + t.end(); + } + + function onReject( error ) { + t.fail( error.message ); + t.end(); + } +}); + +tape( 'the constructor returns a promise which rejects with a provided reason', function test( t ) { + var p = new Promise( executor ); + p.then( onFulfill, onReject ); + + function executor( resolve, reject ) { + reject( new Error( 'beep' ) ); + } + + function onFulfill() { + t.fail( 'should not fulfill' ); + t.end(); + } + + function onReject( error ) { + t.strictEqual( error.message, 'beep', 'returns expected value' ); + t.end(); + } +}); + +tape( 'the constructor has a `resolve` method which returns a fulfilled promise', function test( t ) { + var p = Promise.resolve( 2 ); + p.then( onFulfill, onReject ); + + function onFulfill( value ) { + t.strictEqual( value, 2, 'returns expected value' ); + t.end(); + } + + function onReject( error ) { + t.fail( error.message ); + t.end(); + } +}); + +tape( 'the constructor has a `reject` method which returns a rejected promise', function test( t ) { + var p = Promise.reject( new Error( 'boop' ) ); + p.then( onFulfill, onReject ); + + function onFulfill() { + t.fail( 'should not fulfill' ); + t.end(); + } + + function onReject( error ) { + t.strictEqual( error.message, 'boop', 'returns expected value' ); + t.end(); + } +}); + +tape( 'the `catch` method attaches a rejection handler', function test( t ) { + var p = Promise.reject( new Error( 'bop' ) ); + p.catch( onReject ); + + function onReject( error ) { + t.strictEqual( error.message, 'bop', 'returns expected value' ); + t.end(); + } +}); + +tape( 'if an executor throws an error, the promise is rejected', function test( t ) { + var p = new Promise( executor ); + p.then( onFulfill, onReject ); + + function executor() { + throw new Error( 'oops' ); + } + + function onFulfill() { + t.fail( 'should not fulfill' ); + t.end(); + } + + function onReject( error ) { + t.strictEqual( error.message, 'oops', 'returns expected value' ); + t.end(); + } +}); + +tape( 'the `then` method supports chaining', function test( t ) { + var p = Promise.resolve( 2 ); + p.then( onFulfill ).then( onChained, onReject ); + + function onFulfill( value ) { + return value * 3; + } + + function onChained( value ) { + t.strictEqual( value, 6, 'returns expected value' ); + t.end(); + } + + function onReject( error ) { + t.fail( error.message ); + t.end(); + } +}); + +tape( 'resolving with a thenable adopts the thenable state', function test( t ) { + var thenable; + var p; + + thenable = { + 'then': then + }; + p = Promise.resolve( thenable ); + p.then( onFulfill, onReject ); + + function then( resolve ) { + resolve( 'adopted' ); + } + + function onFulfill( value ) { + t.strictEqual( value, 'adopted', 'returns expected value' ); + t.end(); + } + + function onReject( error ) { + t.fail( error.message ); + t.end(); + } +}); + +tape( 'resolving a promise with itself rejects the promise', function test( t ) { + var resolve; + var p; + + p = new Promise( executor ); + p.then( onFulfill, onReject ); + resolve( p ); + + function executor( res ) { + resolve = res; + } + + function onFulfill() { + t.fail( 'should not fulfill' ); + t.end(); + } + + function onReject( error ) { + t.strictEqual( error instanceof TypeError, true, 'returns expected value' ); + t.end(); + } +}); + +tape( 'subsequent resolve and reject calls are ignored after settlement', function test( t ) { + var p = new Promise( executor ); + p.then( onFulfill, onReject ); + + function executor( resolve, reject ) { + resolve( 1 ); + resolve( 2 ); + reject( new Error( 'beep' ) ); + } + + function onFulfill( value ) { + t.strictEqual( value, 1, 'returns expected value' ); + t.end(); + } + + function onReject( error ) { + t.fail( error.message ); + t.end(); + } +}); diff --git a/lib/node_modules/@stdlib/promise/ctor/test/test.js b/lib/node_modules/@stdlib/promise/ctor/test/test.js new file mode 100644 index 000000000000..2f7de6deef8b --- /dev/null +++ b/lib/node_modules/@stdlib/promise/ctor/test/test.js @@ -0,0 +1,81 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var proxyquire = require( 'proxyquire' ); +var hasPromiseSupport = require( '@stdlib/assert/has-promise-support' ); +var GlobalPromise = require( './../lib/promise.js' ); +var polyfill = require( './../lib/polyfill' ); +var ctor = require( './../lib' ); + + +// VARIABLES // + +var hasPromise = hasPromiseSupport(); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof ctor, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'if an environment supports `Promise`, the export is an alias for `Promise`', function test( t ) { + var Foo; + + Foo = proxyquire( './../lib', { + '@stdlib/assert/has-promise-support': isTrue, + './main.js': Mock + }); + t.strictEqual( Foo, Mock, 'returns builtin' ); + + if ( hasPromise ) { + t.strictEqual( ctor, GlobalPromise, 'is alias' ); + } + + t.end(); + + function Mock() { + return this; + } + + function isTrue() { + return true; + } +}); + +tape( 'if an environment does not support `Promise`, the export is a polyfill', function test( t ) { + var Foo; + + Foo = proxyquire( './../lib', { + '@stdlib/assert/has-promise-support': isFalse + }); + + t.strictEqual( Foo, polyfill, 'returns polyfill' ); + t.end(); + + function isFalse() { + return false; + } +}); diff --git a/lib/node_modules/@stdlib/promise/ctor/test/test.main.js b/lib/node_modules/@stdlib/promise/ctor/test/test.main.js new file mode 100644 index 000000000000..f60196b8d200 --- /dev/null +++ b/lib/node_modules/@stdlib/promise/ctor/test/test.main.js @@ -0,0 +1,70 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var hasPromiseSupport = require( '@stdlib/assert/has-promise-support' ); +var Promise = require( './../lib/main.js' ); + + +// VARIABLES // + +var opts = { + 'skip': !hasPromiseSupport() +}; + + +// TESTS // + +tape( 'main export is a function', opts, function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof Promise, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function is a constructor which returns a promise', opts, function test( t ) { + var p = new Promise( executor ); + t.strictEqual( typeof p.then, 'function', 'returns expected value' ); + t.end(); + + function executor( resolve ) { + resolve( 1 ); + } +}); + +tape( 'the constructor returns a promise which fulfills with a provided value', opts, function test( t ) { + var p = new Promise( executor ); + p.then( onFulfill, onReject ); + + function executor( resolve ) { + resolve( 1 ); + } + + function onFulfill( value ) { + t.strictEqual( value, 1, 'returns expected value' ); + t.end(); + } + + function onReject( error ) { + t.fail( error.message ); + t.end(); + } +}); From 90a40fa5b3da7fa149a91d69e310db28c7905fd9 Mon Sep 17 00:00:00 2001 From: 0PrashantYadav0 Date: Tue, 11 Aug 2026 11:35:52 +0530 Subject: [PATCH 7/9] feat: use promise in module-wrapper --- lib/node_modules/@stdlib/wasm/module-wrapper/lib/main.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/node_modules/@stdlib/wasm/module-wrapper/lib/main.js b/lib/node_modules/@stdlib/wasm/module-wrapper/lib/main.js index 0acbc7bde27a..c3f58d8399c2 100644 --- a/lib/node_modules/@stdlib/wasm/module-wrapper/lib/main.js +++ b/lib/node_modules/@stdlib/wasm/module-wrapper/lib/main.js @@ -39,6 +39,7 @@ var readDataView = require( '@stdlib/strided/base/read-dataview' ).ndarray; var dtype = require( '@stdlib/array/dtype' ); var dtype2wasm = require( '@stdlib/wasm/base/dtype2wasm' ); var format = require( '@stdlib/string/format' ); +var Promise = require( '@stdlib/promise/ctor' ); // VARIABLES // @@ -172,8 +173,7 @@ setReadOnly( WasmModule.prototype, 'initialize', function initialize() { } else { fcn = instantiate; } - // FIXME: replace with `@stdlib/promise/ctor` - return new Promise( fcn ); // eslint-disable-line stdlib/require-globals + return new Promise( fcn ); /** * Returns a WebAssembly instance. From 2a990547a5532c80bb66f37e8d89a86703228807 Mon Sep 17 00:00:00 2001 From: 0PrashantYadav0 Date: Tue, 11 Aug 2026 11:38:39 +0530 Subject: [PATCH 8/9] fix: remove polyfill --- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown_pkg_readmes status: na - task: lint_markdown_docs status: na - task: lint_markdown status: na - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: na - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: na - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: na - task: lint_license_headers status: passed --- --- .../@stdlib/promise/polyfill/README.md | 134 --------- .../promise/polyfill/benchmark/benchmark.js | 51 ---- .../@stdlib/promise/polyfill/docs/repl.txt | 23 -- .../promise/polyfill/docs/types/index.d.ts | 43 --- .../promise/polyfill/docs/types/test.ts | 43 --- .../promise/polyfill/examples/index.js | 58 ---- .../@stdlib/promise/polyfill/lib/fulfill.js | 52 ---- .../@stdlib/promise/polyfill/lib/fulfilled.js | 35 --- .../@stdlib/promise/polyfill/lib/handle.js | 70 ----- .../@stdlib/promise/polyfill/lib/index.js | 50 ---- .../@stdlib/promise/polyfill/lib/main.js | 231 --------------- .../@stdlib/promise/polyfill/lib/pending.js | 35 --- .../@stdlib/promise/polyfill/lib/reject.js | 52 ---- .../@stdlib/promise/polyfill/lib/rejected.js | 35 --- .../@stdlib/promise/polyfill/lib/resolve.js | 110 ------- .../@stdlib/promise/polyfill/package.json | 62 ---- .../@stdlib/promise/polyfill/test/test.js | 271 ------------------ 17 files changed, 1355 deletions(-) delete mode 100644 lib/node_modules/@stdlib/promise/polyfill/README.md delete mode 100644 lib/node_modules/@stdlib/promise/polyfill/benchmark/benchmark.js delete mode 100644 lib/node_modules/@stdlib/promise/polyfill/docs/repl.txt delete mode 100644 lib/node_modules/@stdlib/promise/polyfill/docs/types/index.d.ts delete mode 100644 lib/node_modules/@stdlib/promise/polyfill/docs/types/test.ts delete mode 100644 lib/node_modules/@stdlib/promise/polyfill/examples/index.js delete mode 100644 lib/node_modules/@stdlib/promise/polyfill/lib/fulfill.js delete mode 100644 lib/node_modules/@stdlib/promise/polyfill/lib/fulfilled.js delete mode 100644 lib/node_modules/@stdlib/promise/polyfill/lib/handle.js delete mode 100644 lib/node_modules/@stdlib/promise/polyfill/lib/index.js delete mode 100644 lib/node_modules/@stdlib/promise/polyfill/lib/main.js delete mode 100644 lib/node_modules/@stdlib/promise/polyfill/lib/pending.js delete mode 100644 lib/node_modules/@stdlib/promise/polyfill/lib/reject.js delete mode 100644 lib/node_modules/@stdlib/promise/polyfill/lib/rejected.js delete mode 100644 lib/node_modules/@stdlib/promise/polyfill/lib/resolve.js delete mode 100644 lib/node_modules/@stdlib/promise/polyfill/package.json delete mode 100644 lib/node_modules/@stdlib/promise/polyfill/test/test.js diff --git a/lib/node_modules/@stdlib/promise/polyfill/README.md b/lib/node_modules/@stdlib/promise/polyfill/README.md deleted file mode 100644 index daa67dbed924..000000000000 --- a/lib/node_modules/@stdlib/promise/polyfill/README.md +++ /dev/null @@ -1,134 +0,0 @@ - - -# Promise Polyfill - -> [Promise][mdn-promise] constructor polyfill. - - - -
- -
- - - - - -
- -## Usage - -```javascript -var Promise = require( '@stdlib/promise/polyfill' ); -``` - -#### Promise( executor ) - -Returns a new [Promise][mdn-promise]. - -```javascript -function executor( resolve, reject ) { - resolve( 'beep' ); -} - -var p = new Promise( executor ); - -p.then( onResolve ); - -function onResolve( value ) { - console.log( value ); - // => 'beep' -} -``` - -The `executor` function is invoked immediately with two arguments: - -- **resolve**: callback to invoke upon fulfilling a promise. -- **reject**: callback to invoke upon rejecting a promise. - -
- - - - - -
- -## Notes - -- This package provides a constructor-focused [Promise][mdn-promise] polyfill supporting `then`, `catch`, `Promise.resolve`, and `Promise.reject`. It is intended as a fallback for environments without native Promise support (for example, via [`@stdlib/promise/ctor`][@stdlib/promise/ctor]). -- Asynchronous settlement is scheduled with [`@stdlib/utils/next-tick`][@stdlib/utils/next-tick], which depends on `process.nextTick`. -- Rejected promises without an attached rejection handler do not emit an `unhandledRejection` event. - -
- - - - - -
- -## Examples - -```javascript -var Promise = require( '@stdlib/promise/polyfill' ); - -function executor( resolve ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - resolve( 'beep' ); - } -} - -var p = new Promise( executor ); - -p.then( onResolve ); - -function onResolve( value ) { - console.log( value ); -} -``` - -
- - - - - - - - - - - - - - diff --git a/lib/node_modules/@stdlib/promise/polyfill/benchmark/benchmark.js b/lib/node_modules/@stdlib/promise/polyfill/benchmark/benchmark.js deleted file mode 100644 index 5f9274fac288..000000000000 --- a/lib/node_modules/@stdlib/promise/polyfill/benchmark/benchmark.js +++ /dev/null @@ -1,51 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2026 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var bench = require( '@stdlib/bench' ); -var pkg = require( './../package.json' ).name; -var Promise = require( './../lib' ); - - -// MAIN // - -bench( pkg, function benchmark( b ) { - var p; - var i; - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - p = new Promise( executor ); - if ( typeof p.then !== 'function' ) { - b.fail( 'should return a promise' ); - } - } - b.toc(); - if ( typeof p.then !== 'function' ) { - b.fail( 'should return a promise' ); - } - b.pass( 'benchmark finished' ); - b.end(); - - function executor( resolve ) { - resolve( i ); - } -}); diff --git a/lib/node_modules/@stdlib/promise/polyfill/docs/repl.txt b/lib/node_modules/@stdlib/promise/polyfill/docs/repl.txt deleted file mode 100644 index 3f3522810945..000000000000 --- a/lib/node_modules/@stdlib/promise/polyfill/docs/repl.txt +++ /dev/null @@ -1,23 +0,0 @@ - -{{alias}}( executor ) - Promise constructor polyfill. - - Parameters - ---------- - executor: Function - Function invoked immediately with `resolve` and `reject` callbacks. - - Returns - ------- - out: Promise - Promise instance. - - Examples - -------- - > function executor( resolve ) { resolve( 'beep' ); }; - > var p = new {{alias}}( executor ) - - - See Also - -------- - diff --git a/lib/node_modules/@stdlib/promise/polyfill/docs/types/index.d.ts b/lib/node_modules/@stdlib/promise/polyfill/docs/types/index.d.ts deleted file mode 100644 index 26bc0b243e5d..000000000000 --- a/lib/node_modules/@stdlib/promise/polyfill/docs/types/index.d.ts +++ /dev/null @@ -1,43 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2026 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -// TypeScript Version: 4.1 - -// EXPORTS // - -/** -* Promise constructor polyfill. -* -* @param executor - function invoked immediately with `resolve` and `reject` callbacks -* @returns promise -* -* @example -* function executor( resolve, reject ) { -* resolve( 'beep' ); -* } -* -* var p = new Promise( executor ); -* -* p.then( onResolve ); -* -* function onResolve( value ) { -* console.log( value ); -* // => 'beep' -* } -*/ -export = Promise; diff --git a/lib/node_modules/@stdlib/promise/polyfill/docs/types/test.ts b/lib/node_modules/@stdlib/promise/polyfill/docs/types/test.ts deleted file mode 100644 index 18a21ad73306..000000000000 --- a/lib/node_modules/@stdlib/promise/polyfill/docs/types/test.ts +++ /dev/null @@ -1,43 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2026 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -/* eslint-disable @typescript-eslint/no-unused-expressions, @typescript-eslint/no-floating-promises */ - -import Promise = require( './index' ); - -/** -* Promise executor. -* -* @param resolve - resolve callback -*/ -function executor( resolve: ( value: number ) => void ): void { - resolve( 1 ); -} - - -// TESTS // - -// The function returns a promise... -{ - new Promise( executor ); // $ExpectType Promise -} - -// The constructor function has to be invoked with `new`... -{ - Promise( executor ); // $ExpectError -} diff --git a/lib/node_modules/@stdlib/promise/polyfill/examples/index.js b/lib/node_modules/@stdlib/promise/polyfill/examples/index.js deleted file mode 100644 index 510c671997f3..000000000000 --- a/lib/node_modules/@stdlib/promise/polyfill/examples/index.js +++ /dev/null @@ -1,58 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2026 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -var Promise = require( './../lib' ); - -function main() { - var p = new Promise( executor ); - p.then( onResolve, onReject ); -} - -/** -* Promise executor. -* -* @private -* @param {Function} resolve - callback to invoke upon fulfilling a promise -*/ -function executor( resolve ) { - resolve( 'beep' ); -} - -/** -* Callback invoked upon promise fulfillment. -* -* @private -* @param {*} value - resolved value -*/ -function onResolve( value ) { - console.log( value ); -} - -/** -* Callback invoked upon promise rejection. -* -* @private -* @param {Error} error - error -*/ -function onReject( error ) { - console.error( error.message ); -} - -main(); diff --git a/lib/node_modules/@stdlib/promise/polyfill/lib/fulfill.js b/lib/node_modules/@stdlib/promise/polyfill/lib/fulfill.js deleted file mode 100644 index 1ade794d1ba7..000000000000 --- a/lib/node_modules/@stdlib/promise/polyfill/lib/fulfill.js +++ /dev/null @@ -1,52 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2026 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -/* eslint-disable no-underscore-dangle */ - -'use strict'; - -// MODULES // - -var FULFILLED = require( './fulfilled.js' ); -var handle = require( './handle.js' ); -var PENDING = require( './pending.js' ); - - -// MAIN // - -/** -* Fulfills a promise. -* -* @private -* @param {Object} promise - promise -* @param {*} value - fulfillment value -* @returns {void} -*/ -function fulfill( promise, value ) { - if ( promise._state !== PENDING ) { - return; - } - promise._state = FULFILLED; - promise._value = value; - handle( promise ); -} - - -// EXPORTS // - -module.exports = fulfill; diff --git a/lib/node_modules/@stdlib/promise/polyfill/lib/fulfilled.js b/lib/node_modules/@stdlib/promise/polyfill/lib/fulfilled.js deleted file mode 100644 index 4ad0ef61fd5c..000000000000 --- a/lib/node_modules/@stdlib/promise/polyfill/lib/fulfilled.js +++ /dev/null @@ -1,35 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2026 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MAIN // - -/** -* Fulfilled state. -* -* @private -* @constant -* @type {number} -*/ -var FULFILLED = 1; - - -// EXPORTS // - -module.exports = FULFILLED; diff --git a/lib/node_modules/@stdlib/promise/polyfill/lib/handle.js b/lib/node_modules/@stdlib/promise/polyfill/lib/handle.js deleted file mode 100644 index b48d24cff71f..000000000000 --- a/lib/node_modules/@stdlib/promise/polyfill/lib/handle.js +++ /dev/null @@ -1,70 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2026 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -/* eslint-disable no-underscore-dangle */ - -'use strict'; - -// MODULES // - -var nextTick = require( '@stdlib/utils/next-tick' ); -var PENDING = require( './pending.js' ); -var FULFILLED = require( './fulfilled.js' ); - - -// MAIN // - -/** -* Schedules invocation of queued promise handlers. -* -* @private -* @param {Object} promise - promise -*/ -function handle( promise ) { - if ( promise._state === PENDING ) { - return; - } - nextTick( run ); - - /** - * Invokes queued handlers. - * - * @private - */ - function run() { - var handlers; - var handler; - var i; - - handlers = promise._handlers; - promise._handlers = []; - for ( i = 0; i < handlers.length; i++ ) { - handler = handlers[ i ]; - if ( promise._state === FULFILLED ) { - handler.onFulfilled( promise._value ); - } else { - handler.onRejected( promise._value ); - } - } - } -} - - -// EXPORTS // - -module.exports = handle; diff --git a/lib/node_modules/@stdlib/promise/polyfill/lib/index.js b/lib/node_modules/@stdlib/promise/polyfill/lib/index.js deleted file mode 100644 index 458410d20fcf..000000000000 --- a/lib/node_modules/@stdlib/promise/polyfill/lib/index.js +++ /dev/null @@ -1,50 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2026 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -/** -* Promise constructor polyfill. -* -* @module @stdlib/promise/polyfill -* -* @example -* var Promise = require( '@stdlib/promise/polyfill' ); -* -* function executor( resolve, reject ) { -* resolve( 'beep' ); -* } -* -* var p = new Promise( executor ); -* -* p.then( onResolve ); -* -* function onResolve( value ) { -* console.log( value ); -* // => 'beep' -* } -*/ - -// MODULES // - -var main = require( './main.js' ); - - -// EXPORTS // - -module.exports = main; diff --git a/lib/node_modules/@stdlib/promise/polyfill/lib/main.js b/lib/node_modules/@stdlib/promise/polyfill/lib/main.js deleted file mode 100644 index 3a275acc96c1..000000000000 --- a/lib/node_modules/@stdlib/promise/polyfill/lib/main.js +++ /dev/null @@ -1,231 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2026 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -/* eslint-disable no-restricted-syntax, no-invalid-this, no-underscore-dangle */ - -'use strict'; - -// MODULES // - -var isFunction = require( '@stdlib/assert/is-function' ); -var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); -var format = require( '@stdlib/string/format' ); -var handle = require( './handle.js' ); -var resolve = require( './resolve.js' ); -var reject = require( './reject.js' ); -var PENDING = require( './pending.js' ); - - -// MAIN // - -/** -* Promise constructor polyfill. -* -* @constructor -* @param {Function} executor - function invoked immediately with `resolve` and `reject` callbacks -* @throws {TypeError} must invoke with the `new` keyword -* @throws {TypeError} must provide an executor function -* @returns {Promise} promise instance -* -* @example -* function executor( resolve ) { -* resolve( 'beep' ); -* } -* -* var p = new Promise( executor ); -* -* p.then( onResolve ); -* -* function onResolve( value ) { -* console.log( value ); -* // => 'beep' -* } -*/ -function Promise( executor ) { - var self; - if ( !( this instanceof Promise ) ) { - throw new TypeError( format( 'invalid invocation. Constructor must be called with the `new` operator.' ) ); - } - if ( !isFunction( executor ) ) { - throw new TypeError( format( 'invalid argument. Must provide a function. Value: `%s`.', executor ) ); - } - self = this; - this._state = PENDING; - this._value = void 0; - this._handlers = []; - try { - executor( onResolve, onReject ); - } catch ( err ) { - onReject( err ); - } - return this; - - /** - * Resolves a promise. - * - * @private - * @param {*} value - resolution value - */ - function onResolve( value ) { - resolve( self, value ); - } - - /** - * Rejects a promise. - * - * @private - * @param {*} reason - rejection reason - */ - function onReject( reason ) { - if ( self._state !== PENDING ) { - return; - } - reject( self, reason ); - } -} - -/** -* Attaches fulfillment and rejection handlers to a promise. -* -* @name then -* @memberof Promise.prototype -* @type {Function} -* @param {Function} [onFulfilled] - fulfillment handler -* @param {Function} [onRejected] - rejection handler -* @returns {Promise} new promise -*/ -setReadOnly( Promise.prototype, 'then', function then( onFulfilled, onRejected ) { - var self = this; - return new Promise( executor ); - - /** - * Promise executor. - * - * @private - * @param {Function} resolveValue - resolve callback - * @param {Function} rejectValue - reject callback - */ - function executor( resolveValue, rejectValue ) { - self._handlers.push({ - 'onFulfilled': createHandler( onFulfilled, true ), - 'onRejected': createHandler( onRejected, false ) - }); - handle( self ); - - /** - * Returns a handler wrapper. - * - * @private - * @param {Function} cb - callback - * @param {boolean} isFulfilled - boolean indicating whether the callback is a fulfillment handler - * @returns {Function} handler - */ - function createHandler( cb, isFulfilled ) { - return wrapper; - - /** - * Invokes a settlement handler. - * - * @private - * @param {*} value - settlement value - * @returns {void} - */ - function wrapper( value ) { - if ( !isFunction( cb ) ) { - if ( isFulfilled ) { - return resolveValue( value ); - } - return rejectValue( value ); - } - try { - return resolveValue( cb( value ) ); - } catch ( err ) { - return rejectValue( err ); - } - } - } - } -}); - -/** -* Attaches a rejection handler to a promise. -* -* @name catch -* @memberof Promise.prototype -* @type {Function} -* @param {Function} onRejected - rejection handler -* @returns {Promise} new promise -*/ -setReadOnly( Promise.prototype, 'catch', function promiseCatch( onRejected ) { - return this.then( null, onRejected ); -}); - -/** -* Returns a promise resolved with a provided value. -* -* @name resolve -* @memberof Promise -* @type {Function} -* @param {*} value - resolution value -* @returns {Promise} promise -*/ -setReadOnly( Promise, 'resolve', function promiseResolve( value ) { - if ( value instanceof Promise ) { - return value; - } - return new Promise( executor ); - - /** - * Promise executor. - * - * @private - * @param {Function} resolveValue - resolve callback - */ - function executor( resolveValue ) { - resolveValue( value ); - } -}); - -/** -* Returns a promise rejected with a provided reason. -* -* @name reject -* @memberof Promise -* @type {Function} -* @param {*} reason - rejection reason -* @returns {Promise} promise -*/ -setReadOnly( Promise, 'reject', function promiseReject( reason ) { - return new Promise( executor ); - - /** - * Promise executor. - * - * @private - * @param {Function} resolveValue - resolve callback - * @param {Function} rejectValue - reject callback - */ - function executor( resolveValue, rejectValue ) { - rejectValue( reason ); - } -}); - - -// EXPORTS // - -module.exports = Promise; diff --git a/lib/node_modules/@stdlib/promise/polyfill/lib/pending.js b/lib/node_modules/@stdlib/promise/polyfill/lib/pending.js deleted file mode 100644 index 5c09cf8f9f50..000000000000 --- a/lib/node_modules/@stdlib/promise/polyfill/lib/pending.js +++ /dev/null @@ -1,35 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2026 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MAIN // - -/** -* Pending state. -* -* @private -* @constant -* @type {number} -*/ -var PENDING = 0; - - -// EXPORTS // - -module.exports = PENDING; diff --git a/lib/node_modules/@stdlib/promise/polyfill/lib/reject.js b/lib/node_modules/@stdlib/promise/polyfill/lib/reject.js deleted file mode 100644 index 1bc4050c353b..000000000000 --- a/lib/node_modules/@stdlib/promise/polyfill/lib/reject.js +++ /dev/null @@ -1,52 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2026 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -/* eslint-disable no-underscore-dangle */ - -'use strict'; - -// MODULES // - -var handle = require( './handle.js' ); -var PENDING = require( './pending.js' ); -var REJECTED = require( './rejected.js' ); - - -// MAIN // - -/** -* Rejects a promise. -* -* @private -* @param {Object} promise - promise -* @param {*} reason - rejection reason -* @returns {void} -*/ -function reject( promise, reason ) { - if ( promise._state !== PENDING ) { - return; - } - promise._state = REJECTED; - promise._value = reason; - handle( promise ); -} - - -// EXPORTS // - -module.exports = reject; diff --git a/lib/node_modules/@stdlib/promise/polyfill/lib/rejected.js b/lib/node_modules/@stdlib/promise/polyfill/lib/rejected.js deleted file mode 100644 index 505e38a969cd..000000000000 --- a/lib/node_modules/@stdlib/promise/polyfill/lib/rejected.js +++ /dev/null @@ -1,35 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2026 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MAIN // - -/** -* Rejected state. -* -* @private -* @constant -* @type {number} -*/ -var REJECTED = 2; - - -// EXPORTS // - -module.exports = REJECTED; diff --git a/lib/node_modules/@stdlib/promise/polyfill/lib/resolve.js b/lib/node_modules/@stdlib/promise/polyfill/lib/resolve.js deleted file mode 100644 index 8544942e4974..000000000000 --- a/lib/node_modules/@stdlib/promise/polyfill/lib/resolve.js +++ /dev/null @@ -1,110 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2026 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -/* eslint-disable no-underscore-dangle */ - -'use strict'; - -// MODULES // - -var isFunction = require( '@stdlib/assert/is-function' ); -var format = require( '@stdlib/string/format' ); -var fulfill = require( './fulfill.js' ); -var reject = require( './reject.js' ); -var PENDING = require( './pending.js' ); - - -// MAIN // - -/** -* Resolves a promise according to the Promises/A+ specification. -* -* @private -* @param {Object} promise - promise -* @param {*} value - resolution value -* @returns {void} -*/ -function resolve( promise, value ) { - var called; - var then; - - if ( promise._state !== PENDING ) { - return; - } - if ( value === promise ) { - reject( promise, new TypeError( format( 'invalid value. A promise cannot be resolved with itself. Value: `%s`.', value ) ) ); - return; - } - if ( - value !== null && - ( typeof value === 'object' || typeof value === 'function' ) - ) { - try { - then = value.then; - } catch ( err ) { - reject( promise, err ); - return; - } - if ( isFunction( then ) ) { - called = false; - try { - then.call( value, onFulfilled, onRejected ); - } catch ( err ) { - if ( !called ) { - called = true; - reject( promise, err ); - } - } - return; - } - } - fulfill( promise, value ); - - /** - * Callback invoked upon thenable fulfillment. - * - * @private - * @param {*} v - fulfillment value - */ - function onFulfilled( v ) { - if ( called ) { - return; - } - called = true; - resolve( promise, v ); - } - - /** - * Callback invoked upon thenable rejection. - * - * @private - * @param {*} r - rejection reason - */ - function onRejected( r ) { - if ( called ) { - return; - } - called = true; - reject( promise, r ); - } -} - - -// EXPORTS // - -module.exports = resolve; diff --git a/lib/node_modules/@stdlib/promise/polyfill/package.json b/lib/node_modules/@stdlib/promise/polyfill/package.json deleted file mode 100644 index 5b2d7fbadadf..000000000000 --- a/lib/node_modules/@stdlib/promise/polyfill/package.json +++ /dev/null @@ -1,62 +0,0 @@ -{ - "name": "@stdlib/promise/polyfill", - "version": "0.0.0", - "description": "Promise constructor polyfill.", - "license": "Apache-2.0", - "author": { - "name": "The Stdlib Authors", - "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" - }, - "contributors": [ - { - "name": "The Stdlib Authors", - "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" - } - ], - "main": "./lib", - "directories": { - "benchmark": "./benchmark", - "doc": "./docs", - "example": "./examples", - "lib": "./lib", - "test": "./test" - }, - "types": "./docs/types", - "scripts": {}, - "homepage": "https://github.com/stdlib-js/stdlib", - "repository": { - "type": "git", - "url": "git://github.com/stdlib-js/stdlib.git" - }, - "bugs": { - "url": "https://github.com/stdlib-js/stdlib/issues" - }, - "dependencies": {}, - "devDependencies": {}, - "engines": { - "node": ">=0.10.0", - "npm": ">2.7.0" - }, - "os": [ - "aix", - "darwin", - "freebsd", - "linux", - "macos", - "openbsd", - "sunos", - "win32", - "windows" - ], - "keywords": [ - "stdlib", - "stdtypes", - "types", - "promise", - "promises", - "polyfill", - "ponyfill", - "es2015", - "es6" - ] -} diff --git a/lib/node_modules/@stdlib/promise/polyfill/test/test.js b/lib/node_modules/@stdlib/promise/polyfill/test/test.js deleted file mode 100644 index c4749df06926..000000000000 --- a/lib/node_modules/@stdlib/promise/polyfill/test/test.js +++ /dev/null @@ -1,271 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2026 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var Promise = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof Promise, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function throws an error if not invoked with the `new` keyword', function test( t ) { - t.throws( foo, TypeError, 'throws an error' ); - t.end(); - - function foo() { - Promise( executor ); // eslint-disable-line new-cap - } - - function executor( resolve ) { - resolve( 1 ); - } -}); - -tape( 'the function throws an error if not provided a function', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - [], - {} - ]; - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided '+values[ i ] ); - } - t.end(); - - function badValue( value ) { - return wrapper; - - function wrapper() { - var p = new Promise( value ); // eslint-disable-line no-unused-vars - } - } -}); - -tape( 'the function is a constructor which returns a promise', function test( t ) { - var p = new Promise( executor ); - t.strictEqual( typeof p.then, 'function', 'returns expected value' ); - t.strictEqual( typeof p.catch, 'function', 'returns expected value' ); - t.end(); - - function executor( resolve ) { - resolve( 1 ); - } -}); - -tape( 'the constructor returns a promise which fulfills with a provided value', function test( t ) { - var p = new Promise( executor ); - p.then( onFulfill, onReject ); - - function executor( resolve ) { - resolve( 1 ); - } - - function onFulfill( value ) { - t.strictEqual( value, 1, 'returns expected value' ); - t.end(); - } - - function onReject( error ) { - t.fail( error.message ); - t.end(); - } -}); - -tape( 'the constructor returns a promise which rejects with a provided reason', function test( t ) { - var p = new Promise( executor ); - p.then( onFulfill, onReject ); - - function executor( resolve, reject ) { - reject( new Error( 'beep' ) ); - } - - function onFulfill() { - t.fail( 'should not fulfill' ); - t.end(); - } - - function onReject( error ) { - t.strictEqual( error.message, 'beep', 'returns expected value' ); - t.end(); - } -}); - -tape( 'the constructor has a `resolve` method which returns a fulfilled promise', function test( t ) { - var p = Promise.resolve( 2 ); - p.then( onFulfill, onReject ); - - function onFulfill( value ) { - t.strictEqual( value, 2, 'returns expected value' ); - t.end(); - } - - function onReject( error ) { - t.fail( error.message ); - t.end(); - } -}); - -tape( 'the constructor has a `reject` method which returns a rejected promise', function test( t ) { - var p = Promise.reject( new Error( 'boop' ) ); - p.then( onFulfill, onReject ); - - function onFulfill() { - t.fail( 'should not fulfill' ); - t.end(); - } - - function onReject( error ) { - t.strictEqual( error.message, 'boop', 'returns expected value' ); - t.end(); - } -}); - -tape( 'the `catch` method attaches a rejection handler', function test( t ) { - var p = Promise.reject( new Error( 'bop' ) ); - p.catch( onReject ); - - function onReject( error ) { - t.strictEqual( error.message, 'bop', 'returns expected value' ); - t.end(); - } -}); - -tape( 'if an executor throws an error, the promise is rejected', function test( t ) { - var p = new Promise( executor ); - p.then( onFulfill, onReject ); - - function executor() { - throw new Error( 'oops' ); - } - - function onFulfill() { - t.fail( 'should not fulfill' ); - t.end(); - } - - function onReject( error ) { - t.strictEqual( error.message, 'oops', 'returns expected value' ); - t.end(); - } -}); - -tape( 'the `then` method supports chaining', function test( t ) { - var p = Promise.resolve( 2 ); - p.then( onFulfill ).then( onChained, onReject ); - - function onFulfill( value ) { - return value * 3; - } - - function onChained( value ) { - t.strictEqual( value, 6, 'returns expected value' ); - t.end(); - } - - function onReject( error ) { - t.fail( error.message ); - t.end(); - } -}); - -tape( 'resolving with a thenable adopts the thenable state', function test( t ) { - var thenable; - var p; - - thenable = { - 'then': then - }; - p = Promise.resolve( thenable ); - p.then( onFulfill, onReject ); - - function then( resolve ) { - resolve( 'adopted' ); - } - - function onFulfill( value ) { - t.strictEqual( value, 'adopted', 'returns expected value' ); - t.end(); - } - - function onReject( error ) { - t.fail( error.message ); - t.end(); - } -}); - -tape( 'resolving a promise with itself rejects the promise', function test( t ) { - var resolve; - var p; - - p = new Promise( executor ); - p.then( onFulfill, onReject ); - resolve( p ); - - function executor( res ) { - resolve = res; - } - - function onFulfill() { - t.fail( 'should not fulfill' ); - t.end(); - } - - function onReject( error ) { - t.strictEqual( error instanceof TypeError, true, 'returns expected value' ); - t.end(); - } -}); - -tape( 'subsequent resolve and reject calls are ignored after settlement', function test( t ) { - var p = new Promise( executor ); - p.then( onFulfill, onReject ); - - function executor( resolve, reject ) { - resolve( 1 ); - resolve( 2 ); - reject( new Error( 'beep' ) ); - } - - function onFulfill( value ) { - t.strictEqual( value, 1, 'returns expected value' ); - t.end(); - } - - function onReject( error ) { - t.fail( error.message ); - t.end(); - } -}); From feb9a31e279878962b53ed7ddab8e76e6be052d7 Mon Sep 17 00:00:00 2001 From: 0PrashantYadav0 Date: Tue, 11 Aug 2026 11:41:38 +0530 Subject: [PATCH 9/9] feat: change in test to cover all the test cases --- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown_pkg_readmes status: passed - task: lint_markdown_docs status: na - task: lint_markdown status: na - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: na - task: lint_license_headers status: passed --- --- .../@stdlib/promise/ctor/README.md | 4 - .../@stdlib/promise/ctor/lib/index.js | 8 +- .../@stdlib/promise/ctor/lib/main.js | 16 +-- .../@stdlib/promise/ctor/lib/promise.js | 28 ----- .../@stdlib/promise/ctor/test/test.js | 108 ++++++++++++------ .../@stdlib/promise/ctor/test/test.main.js | 70 ------------ .../promise/ctor/test/test.polyfill.js | 80 +++++++++++++ 7 files changed, 160 insertions(+), 154 deletions(-) delete mode 100644 lib/node_modules/@stdlib/promise/ctor/lib/promise.js delete mode 100644 lib/node_modules/@stdlib/promise/ctor/test/test.main.js create mode 100644 lib/node_modules/@stdlib/promise/ctor/test/test.polyfill.js diff --git a/lib/node_modules/@stdlib/promise/ctor/README.md b/lib/node_modules/@stdlib/promise/ctor/README.md index 7cf51c5b790c..1c49676fb2cb 100644 --- a/lib/node_modules/@stdlib/promise/ctor/README.md +++ b/lib/node_modules/@stdlib/promise/ctor/README.md @@ -72,10 +72,6 @@ The `executor` function is invoked immediately with two arguments:
-## Notes - -- In environments lacking native [`Promise`][mdn-promise] support, the package falls back to a Promise constructor polyfill. -
diff --git a/lib/node_modules/@stdlib/promise/ctor/lib/index.js b/lib/node_modules/@stdlib/promise/ctor/lib/index.js index d633d9241a3f..2f099e1ea893 100644 --- a/lib/node_modules/@stdlib/promise/ctor/lib/index.js +++ b/lib/node_modules/@stdlib/promise/ctor/lib/index.js @@ -49,14 +49,14 @@ var polyfill = require( './polyfill' ); // MAIN // -var main; +var ctor; if ( hasPromiseSupport() ) { - main = builtin; + ctor = builtin; } else { - main = polyfill; + ctor = polyfill; } // EXPORTS // -module.exports = main; +module.exports = ctor; diff --git a/lib/node_modules/@stdlib/promise/ctor/lib/main.js b/lib/node_modules/@stdlib/promise/ctor/lib/main.js index 2c686a11994b..1ecaf737c769 100644 --- a/lib/node_modules/@stdlib/promise/ctor/lib/main.js +++ b/lib/node_modules/@stdlib/promise/ctor/lib/main.js @@ -18,23 +18,11 @@ 'use strict'; -// MODULES // - -var PromiseCtor = require( './promise.js' ); - - // MAIN // -/** -* Promise constructor. -* -* @name Promise -* @constructor -* @type {Function} -*/ -var main = PromiseCtor; +var ctor = ( typeof Promise === 'function' ) ? Promise : void 0; // eslint-disable-line stdlib/require-globals // EXPORTS // -module.exports = main; +module.exports = ctor; diff --git a/lib/node_modules/@stdlib/promise/ctor/lib/promise.js b/lib/node_modules/@stdlib/promise/ctor/lib/promise.js deleted file mode 100644 index 7b3070c04403..000000000000 --- a/lib/node_modules/@stdlib/promise/ctor/lib/promise.js +++ /dev/null @@ -1,28 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2026 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MAIN // - -var main = ( typeof Promise === 'function' ) ? Promise : null; // eslint-disable-line stdlib/require-globals - - -// EXPORTS // - -module.exports = main; diff --git a/lib/node_modules/@stdlib/promise/ctor/test/test.js b/lib/node_modules/@stdlib/promise/ctor/test/test.js index 2f7de6deef8b..18edc348e785 100644 --- a/lib/node_modules/@stdlib/promise/ctor/test/test.js +++ b/lib/node_modules/@stdlib/promise/ctor/test/test.js @@ -21,61 +21,101 @@ // MODULES // var tape = require( 'tape' ); -var proxyquire = require( 'proxyquire' ); -var hasPromiseSupport = require( '@stdlib/assert/has-promise-support' ); -var GlobalPromise = require( './../lib/promise.js' ); -var polyfill = require( './../lib/polyfill' ); -var ctor = require( './../lib' ); - - -// VARIABLES // - -var hasPromise = hasPromiseSupport(); +var Promise = require( './../lib' ); // TESTS // tape( 'main export is a function', function test( t ) { t.ok( true, __filename ); - t.strictEqual( typeof ctor, 'function', 'main export is a function' ); + t.strictEqual( typeof Promise, 'function', 'main export is a function' ); t.end(); }); -tape( 'if an environment supports `Promise`, the export is an alias for `Promise`', function test( t ) { - var Foo; +tape( 'the function is a constructor which returns a promise', function test( t ) { + var p = new Promise( executor ); + t.strictEqual( typeof p.then, 'function', 'returns expected value' ); + t.end(); + + function executor( resolve ) { + resolve( 1 ); + } +}); - Foo = proxyquire( './../lib', { - '@stdlib/assert/has-promise-support': isTrue, - './main.js': Mock - }); - t.strictEqual( Foo, Mock, 'returns builtin' ); +tape( 'the constructor returns a promise which fulfills with a provided value', function test( t ) { + var p = new Promise( executor ); + p.then( onFulfill, onReject ); - if ( hasPromise ) { - t.strictEqual( ctor, GlobalPromise, 'is alias' ); + function executor( resolve ) { + resolve( 1 ); } - t.end(); + function onFulfill( value ) { + t.strictEqual( value, 1, 'returns expected value' ); + t.end(); + } + + function onReject( error ) { + t.fail( error.message ); + t.end(); + } +}); + +tape( 'the constructor returns a promise which rejects with a provided reason', function test( t ) { + var p = new Promise( executor ); + p.then( onFulfill, onReject ); - function Mock() { - return this; + function executor( resolve, reject ) { + reject( new Error( 'beep' ) ); } - function isTrue() { - return true; + function onFulfill() { + t.fail( 'should not fulfill' ); + t.end(); + } + + function onReject( error ) { + t.strictEqual( error.message, 'beep', 'returns expected value' ); + t.end(); } }); -tape( 'if an environment does not support `Promise`, the export is a polyfill', function test( t ) { - var Foo; +tape( 'the constructor has a `resolve` method which returns a fulfilled promise', function test( t ) { + var p = Promise.resolve( 2 ); + p.then( onFulfill, onReject ); - Foo = proxyquire( './../lib', { - '@stdlib/assert/has-promise-support': isFalse - }); + function onFulfill( value ) { + t.strictEqual( value, 2, 'returns expected value' ); + t.end(); + } - t.strictEqual( Foo, polyfill, 'returns polyfill' ); - t.end(); + function onReject( error ) { + t.fail( error.message ); + t.end(); + } +}); + +tape( 'the constructor has a `reject` method which returns a rejected promise', function test( t ) { + var p = Promise.reject( new Error( 'boop' ) ); + p.then( onFulfill, onReject ); + + function onFulfill() { + t.fail( 'should not fulfill' ); + t.end(); + } + + function onReject( error ) { + t.strictEqual( error.message, 'boop', 'returns expected value' ); + t.end(); + } +}); + +tape( 'the `catch` method attaches a rejection handler', function test( t ) { + var p = Promise.reject( new Error( 'bop' ) ); + p.catch( onReject ); - function isFalse() { - return false; + function onReject( error ) { + t.strictEqual( error.message, 'bop', 'returns expected value' ); + t.end(); } }); diff --git a/lib/node_modules/@stdlib/promise/ctor/test/test.main.js b/lib/node_modules/@stdlib/promise/ctor/test/test.main.js deleted file mode 100644 index f60196b8d200..000000000000 --- a/lib/node_modules/@stdlib/promise/ctor/test/test.main.js +++ /dev/null @@ -1,70 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2026 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var hasPromiseSupport = require( '@stdlib/assert/has-promise-support' ); -var Promise = require( './../lib/main.js' ); - - -// VARIABLES // - -var opts = { - 'skip': !hasPromiseSupport() -}; - - -// TESTS // - -tape( 'main export is a function', opts, function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof Promise, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function is a constructor which returns a promise', opts, function test( t ) { - var p = new Promise( executor ); - t.strictEqual( typeof p.then, 'function', 'returns expected value' ); - t.end(); - - function executor( resolve ) { - resolve( 1 ); - } -}); - -tape( 'the constructor returns a promise which fulfills with a provided value', opts, function test( t ) { - var p = new Promise( executor ); - p.then( onFulfill, onReject ); - - function executor( resolve ) { - resolve( 1 ); - } - - function onFulfill( value ) { - t.strictEqual( value, 1, 'returns expected value' ); - t.end(); - } - - function onReject( error ) { - t.fail( error.message ); - t.end(); - } -}); diff --git a/lib/node_modules/@stdlib/promise/ctor/test/test.polyfill.js b/lib/node_modules/@stdlib/promise/ctor/test/test.polyfill.js new file mode 100644 index 000000000000..cf8e953fd681 --- /dev/null +++ b/lib/node_modules/@stdlib/promise/ctor/test/test.polyfill.js @@ -0,0 +1,80 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var proxyquire = require( 'proxyquire' ); +var hasPromiseSupport = require( '@stdlib/assert/has-promise-support' ); +var polyfill = require( './../lib/polyfill' ); +var ctor = require( './../lib' ); + + +// VARIABLES // + +var hasPromise = hasPromiseSupport(); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof ctor, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'if an environment supports `Promise`, the export is an alias for `Promise`', function test( t ) { + var Foo; + + Foo = proxyquire( './../lib', { + '@stdlib/assert/has-promise-support': isTrue, + './main.js': Mock + }); + t.strictEqual( Foo, Mock, 'returns builtin' ); + + if ( hasPromise ) { + t.strictEqual( ctor, Promise, 'is alias' ); // eslint-disable-line stdlib/require-globals + } + + t.end(); + + function Mock() { + return this; + } + + function isTrue() { + return true; + } +}); + +tape( 'if an environment does not support `Promise`, the export is a polyfill', function test( t ) { + var Foo; + + Foo = proxyquire( './../lib', { + '@stdlib/assert/has-promise-support': isFalse + }); + + t.strictEqual( Foo, polyfill, 'returns polyfill' ); + t.end(); + + function isFalse() { + return false; + } +});