Skip to content
Open
124 changes: 124 additions & 0 deletions lib/node_modules/@stdlib/promise/ctor/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<!--

@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.

-->

# Promise

> [Promise][mdn-promise] constructor.

<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->

<section class="intro">

</section>

<!-- /.intro -->

<!-- Package usage documentation. -->

<section class="usage">

## 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.

</section>

<!-- /.usage -->

<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="notes">

</section>

<!-- /.notes -->

<!-- Package usage examples. -->

<section class="examples">

## 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 );
}
```

</section>

<!-- /.examples -->

<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->

<section class="related">

</section>

<!-- /.related -->

<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="links">

[mdn-promise]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise

</section>

<!-- /.links -->
51 changes: 51 additions & 0 deletions lib/node_modules/@stdlib/promise/ctor/benchmark/benchmark.js
Original file line number Diff line number Diff line change
@@ -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 );
}
});
Original file line number Diff line number Diff line change
@@ -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 );
}
});
23 changes: 23 additions & 0 deletions lib/node_modules/@stdlib/promise/ctor/docs/repl.txt
Original file line number Diff line number Diff line change
@@ -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 )
<Promise>

See Also
--------

43 changes: 43 additions & 0 deletions lib/node_modules/@stdlib/promise/ctor/docs/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -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;
43 changes: 43 additions & 0 deletions lib/node_modules/@stdlib/promise/ctor/docs/types/test.ts
Original file line number Diff line number Diff line change
@@ -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<number>
}

// The constructor function has to be invoked with `new`...
{
Promise( executor ); // $ExpectError
}
Loading
Loading