update
This commit is contained in:
8
ts/promise-make-naked/constants.ts
Normal file
8
ts/promise-make-naked/constants.ts
Normal file
@ -0,0 +1,8 @@
|
||||
|
||||
/* MAIN */
|
||||
|
||||
const NOOP = (): void => {};
|
||||
|
||||
/* EXPORT */
|
||||
|
||||
export {NOOP};
|
53
ts/promise-make-naked/index.ts
Executable file
53
ts/promise-make-naked/index.ts
Executable file
@ -0,0 +1,53 @@
|
||||
|
||||
/* IMPORT */
|
||||
|
||||
import {NOOP} from './constants.js';
|
||||
import type {PromiseResolve, PromiseReject, Result} from './types.js';
|
||||
|
||||
/* MAIN */
|
||||
|
||||
const makeNakedPromise = <T> (): Result<T> => {
|
||||
|
||||
let resolve: PromiseResolve<T> = NOOP;
|
||||
let reject: PromiseReject = NOOP;
|
||||
|
||||
let resolved = false;
|
||||
let rejected = false;
|
||||
|
||||
const promise = new Promise<T> ( ( res, rej ): void => {
|
||||
|
||||
resolve = value => {
|
||||
resolved = true;
|
||||
return res ( value );
|
||||
};
|
||||
|
||||
reject = value => {
|
||||
rejected = true;
|
||||
return rej ( value );
|
||||
};
|
||||
|
||||
});
|
||||
|
||||
const isPending = (): boolean => !resolved && !rejected;
|
||||
const isResolved = (): boolean => resolved;
|
||||
const isRejected = (): boolean => rejected;
|
||||
|
||||
return {promise, resolve, reject, isPending, isResolved, isRejected};
|
||||
|
||||
};
|
||||
|
||||
/* UTILITIES */
|
||||
|
||||
makeNakedPromise.wrap = async <T> ( fn: ( result: Result<T> ) => void ): Promise<T> => {
|
||||
|
||||
const result = makeNakedPromise<T> ();
|
||||
|
||||
await fn ( result );
|
||||
|
||||
return result.promise;
|
||||
|
||||
};
|
||||
|
||||
/* EXPORT */
|
||||
|
||||
export default makeNakedPromise;
|
21
ts/promise-make-naked/license
Normal file
21
ts/promise-make-naked/license
Normal file
@ -0,0 +1,21 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2021-present Fabio Spampinato
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a
|
||||
copy of this software and associated documentation files (the "Software"),
|
||||
to deal in the Software without restriction, including without limitation
|
||||
the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
and/or sell copies of the Software, and to permit persons to whom the
|
||||
Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
DEALINGS IN THE SOFTWARE.
|
19
ts/promise-make-naked/types.ts
Normal file
19
ts/promise-make-naked/types.ts
Normal file
@ -0,0 +1,19 @@
|
||||
|
||||
/* MAIN */
|
||||
|
||||
type PromiseResolve <T> = ( value: T | PromiseLike<T> ) => void;
|
||||
|
||||
type PromiseReject = ( reason?: unknown ) => void;
|
||||
|
||||
type Result <T> = {
|
||||
promise: Promise<T>,
|
||||
resolve: PromiseResolve<T>,
|
||||
reject: PromiseReject,
|
||||
isPending: () => boolean,
|
||||
isResolved: () => boolean,
|
||||
isRejected: () => boolean
|
||||
};
|
||||
|
||||
/* EXPORT */
|
||||
|
||||
export type {PromiseResolve, PromiseReject, Result};
|
Reference in New Issue
Block a user