Skip to content

Worldwidebrine/promise-constructor

Repository files navigation

Promise Constructor

Creating Promises in TypeScript.

By syntactic sugar

Without Promise class. Works even if Promise is null.

Async

Promise = null;
globalThis.Promise = null;
const { promise, resolve, reject } = await createNativePromise();

Sync

Contains as unknown as.

Promise = null;
globalThis.Promise = null;
const { promise, resolve, reject } = createNativePromiseSync();

By Promise class/constructor

Async callback

No as unknown as. Note that the callback will always be called asynchronously.

createPromiseAsync((promise, resolve, reject) => {
});

Sync callback

Contains as unknown as. JS dist can't pass //@ts-check.

Note that the callback will always be called synchronously.

createPromiseSync((promise, resolve, reject) => {
});

Promised

No as unknown as. Nested promise.

createPromisePromised().then(({ promise, resolve, reject }) => {
});

Typecasting

The classic way using as unknown as. JS dist can't pass //@ts-check.

var { promise, resolve, reject } = createPromise();