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..1c49676fb2cb --- /dev/null +++ b/lib/node_modules/@stdlib/promise/ctor/README.md @@ -0,0 +1,124 @@ + + +# 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. + +
+ + + + + +
+ +
+ + + + + +
+ +## 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/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 +} 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/lib/index.js b/lib/node_modules/@stdlib/promise/ctor/lib/index.js new file mode 100644 index 000000000000..2f099e1ea893 --- /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 ctor; +if ( hasPromiseSupport() ) { + ctor = builtin; +} else { + ctor = polyfill; +} + + +// EXPORTS // + +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 new file mode 100644 index 000000000000..1ecaf737c769 --- /dev/null +++ b/lib/node_modules/@stdlib/promise/ctor/lib/main.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 ctor = ( typeof Promise === 'function' ) ? Promise : void 0; // eslint-disable-line stdlib/require-globals + + +// EXPORTS // + +module.exports = ctor; 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/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..18edc348e785 --- /dev/null +++ b/lib/node_modules/@stdlib/promise/ctor/test/test.js @@ -0,0 +1,121 @@ +/** +* @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 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 ); + } +}); + +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(); + } +}); 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; + } +}); 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.