promise-make-naked
Advanced tools
Comparing version 2.0.0 to 2.1.0
import type { Result } from './types'; | ||
declare const makeNakedPromise: <T>() => Result<T>; | ||
declare const makeNakedPromise: { | ||
<T>(): Result<T>; | ||
wrap<T_1>(fn: (result: Result<T_1>) => void): Promise<T_1>; | ||
}; | ||
export default makeNakedPromise; |
@@ -7,2 +7,4 @@ /* IMPORT */ | ||
let reject = NOOP; | ||
let resolved = false; | ||
let rejected = false; | ||
const promise = new Promise((res, rej) => { | ||
@@ -12,5 +14,15 @@ resolve = res; | ||
}); | ||
return { promise, resolve, reject }; | ||
promise.then(() => resolved = true, () => rejected = true); | ||
const isPending = () => !resolved && !rejected; | ||
const isResolved = () => resolved; | ||
const isRejected = () => rejected; | ||
return { promise, resolve, reject, isPending, isResolved, isRejected }; | ||
}; | ||
/* UTILITIES */ | ||
makeNakedPromise.wrap = async (fn) => { | ||
const result = makeNakedPromise(); | ||
await fn(result); | ||
return result.promise; | ||
}; | ||
/* EXPORT */ | ||
export default makeNakedPromise; |
@@ -1,8 +0,11 @@ | ||
declare type PromiseResolve<T> = (value: T | PromiseLike<T>) => void; | ||
declare type PromiseReject = (reason?: unknown) => void; | ||
declare type Result<T> = { | ||
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 type { PromiseResolve, PromiseReject, Result }; |
@@ -5,3 +5,3 @@ { | ||
"description": "A simple function that makes a promise that can be resolved or rejected from the outside.", | ||
"version": "2.0.0", | ||
"version": "2.1.0", | ||
"type": "module", | ||
@@ -17,3 +17,3 @@ "main": "dist/index.js", | ||
"test:watch": "tsex test --watch", | ||
"prepublishOnly": "npm run clean && npm run compile && npm run test" | ||
"prepublishOnly": "tsex prepare" | ||
}, | ||
@@ -25,6 +25,6 @@ "keywords": [ | ||
"devDependencies": { | ||
"fava": "^0.0.6", | ||
"tsex": "^1.1.0", | ||
"typescript": "^4.6.3" | ||
"fava": "^0.2.0", | ||
"tsex": "^3.0.1", | ||
"typescript": "^5.1.6" | ||
} | ||
} |
@@ -16,6 +16,20 @@ # Promise Make Naked | ||
const {promise, resolve, reject} = makeNakedPromise (); | ||
// Let's create a naked Promise | ||
const {promise, resolve, reject, isPending, isResolved, isRejected} = makeNakedPromise (); | ||
resolve ( 123 ); // Resolve the promise with the provided value | ||
reject ( new Error () ); // Reject the promise with the provided reason | ||
isPending (); // Check if promise is still pending | ||
isResolved (); // Check if the promise got resolved | ||
isRejected (); // Check if the promise got rejected | ||
// Let's create a naked Promise similarly to how a normal Promise is created | ||
const result = makeNakedPromise.wrap ( ({ promise, resolve, reject, isPending, isResolved, isRejected }) => { | ||
resolve ( 123 ); | ||
}); | ||
``` | ||
@@ -22,0 +36,0 @@ |
@@ -14,2 +14,5 @@ | ||
let resolved = false; | ||
let rejected = false; | ||
const promise = new Promise<T> ( ( res, rej ): void => { | ||
@@ -20,8 +23,26 @@ resolve = res; | ||
return {promise, resolve, reject}; | ||
promise.then ( () => resolved = true, () => rejected = true ); | ||
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; |
@@ -11,3 +11,6 @@ | ||
resolve: PromiseResolve<T>, | ||
reject: PromiseReject | ||
reject: PromiseReject, | ||
isPending: () => boolean, | ||
isResolved: () => boolean, | ||
isRejected: () => boolean | ||
}; | ||
@@ -14,0 +17,0 @@ |
@@ -5,11 +5,12 @@ | ||
import {describe} from 'fava'; | ||
import promiseMakeNaked from '../dist/index.js'; | ||
import {setTimeout as delay} from 'node:timers/promises'; | ||
import makeNakedPromise from '../dist/index.js'; | ||
/* MAIN */ | ||
describe ( 'promiseMakeNaked', it => { | ||
describe ( 'makeNakedPromise', it => { | ||
it ( 'returns a function which can be resolved from the outside', t => { | ||
const {promise, resolve} = promiseMakeNaked (); | ||
const {promise, resolve} = makeNakedPromise (); | ||
@@ -24,3 +25,3 @@ promise.then ( t.pass ); | ||
const {promise, reject} = promiseMakeNaked (); | ||
const {promise, reject} = makeNakedPromise (); | ||
@@ -33,2 +34,107 @@ promise.catch ( t.pass ); | ||
it ( 'returns a function which can be used to check if the promise is pending, resolve branch', async t => { | ||
const {resolve, isPending} = makeNakedPromise (); | ||
t.true ( isPending () ); | ||
resolve (); | ||
await delay ( 50 ); | ||
t.false ( isPending () ); | ||
}); | ||
it ( 'returns a function which can be used to check if the promise is pending, reject branch', async t => { | ||
const {reject, isPending} = makeNakedPromise (); | ||
t.true ( isPending () ); | ||
reject (); | ||
await delay ( 50 ); | ||
t.false ( isPending () ); | ||
}); | ||
it ( 'returns a function which can be used to check if the promise is resolved, resolve branch', async t => { | ||
const {resolve, isResolved} = makeNakedPromise (); | ||
t.false ( isResolved () ); | ||
resolve (); | ||
await delay ( 50 ); | ||
t.true ( isResolved () ); | ||
}); | ||
it ( 'returns a function which can be used to check if the promise is resolved, reject branch', async t => { | ||
const {reject, isResolved} = makeNakedPromise (); | ||
t.false ( isResolved () ); | ||
reject (); | ||
await delay ( 50 ); | ||
t.false ( isResolved () ); | ||
}); | ||
it ( 'returns a function which can be used to check if the promise is rejected, resolve branch', async t => { | ||
const {resolve, isRejected} = makeNakedPromise (); | ||
t.false ( isRejected () ); | ||
resolve (); | ||
await delay ( 50 ); | ||
t.false ( isRejected () ); | ||
}); | ||
it ( 'returns a function which can be used to check if the promise is rejected, reject branch', async t => { | ||
const {reject, isRejected} = makeNakedPromise (); | ||
t.false ( isRejected () ); | ||
reject (); | ||
await delay ( 50 ); | ||
t.true ( isRejected () ); | ||
}); | ||
it ( 'supports wrapping an arbitrary function', async t => { | ||
const promise = makeNakedPromise.wrap ( async result => { | ||
t.true ( result.promise instanceof Promise ); | ||
t.true ( typeof result.resolve === 'function' ); | ||
t.true ( typeof result.reject === 'function' ); | ||
t.true ( typeof result.isPending === 'function' ); | ||
t.true ( typeof result.isResolved === 'function' ); | ||
t.true ( typeof result.isRejected === 'function' ); | ||
await delay ( 50 ); | ||
result.resolve ( 123 ); | ||
}); | ||
t.is ( await promise, 123 ); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
8585
170
39