diff --git a/README.md b/README.md index 4171a1d..30d7adb 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,7 @@ - [to](#to---Simplified-Promise-Handling-with-Tuples) - [delay](#delay---Pause-Execution-for-a-Specified-Time) - [timeout](#timeout---Timeout-a-Promise) +- [deferred](#deferred---Create-a-Deferred-Promise) ## Installation ```sh @@ -94,4 +95,22 @@ const user = await timeout(api.get("/me"), 1000) // Timeout after 1 second // With custom error message const user = await timeout(api.get("/me"), 1000, "Request timed out") +``` + +### `deferred` - Create a Deferred Promise + +The deferred function allows you to manually resolve or reject a promise at a later time. This can be useful in scenarios where you need to control the timing of the resolution or rejection, such as in testing or when dealing with asynchronous operations that don't natively return promises. + +```ts +import { deferred } from "@mrspartak/promises" + +// Create a deferred promise +const { promise, resolve, reject } = deferred() + +setTimeout(() => { + // Resolve the promise + resolve() +}, 1000) + +await promise // Will wait for 1 second before resolving ``` \ No newline at end of file diff --git a/docs/assets/highlight.css b/docs/assets/highlight.css index ef2cd6c..9700e2c 100644 --- a/docs/assets/highlight.css +++ b/docs/assets/highlight.css @@ -17,6 +17,8 @@ --dark-hl-7: #4FC1FF; --light-hl-8: #098658; --dark-hl-8: #B5CEA8; + --light-hl-9: #267F99; + --dark-hl-9: #4EC9B0; --light-code-background: #FFFFFF; --dark-code-background: #1E1E1E; } @@ -31,6 +33,7 @@ --hl-6: var(--light-hl-6); --hl-7: var(--light-hl-7); --hl-8: var(--light-hl-8); + --hl-9: var(--light-hl-9); --code-background: var(--light-code-background); } } @@ -44,6 +47,7 @@ --hl-6: var(--dark-hl-6); --hl-7: var(--dark-hl-7); --hl-8: var(--dark-hl-8); + --hl-9: var(--dark-hl-9); --code-background: var(--dark-code-background); } } @@ -57,6 +61,7 @@ --hl-6: var(--light-hl-6); --hl-7: var(--light-hl-7); --hl-8: var(--light-hl-8); + --hl-9: var(--light-hl-9); --code-background: var(--light-code-background); } @@ -70,6 +75,7 @@ --hl-6: var(--dark-hl-6); --hl-7: var(--dark-hl-7); --hl-8: var(--dark-hl-8); + --hl-9: var(--dark-hl-9); --code-background: var(--dark-code-background); } @@ -82,4 +88,5 @@ .hl-6 { color: var(--hl-6); } .hl-7 { color: var(--hl-7); } .hl-8 { color: var(--hl-8); } +.hl-9 { color: var(--hl-9); } pre, code { background: var(--code-background); } diff --git a/docs/assets/navigation.js b/docs/assets/navigation.js index e9825c2..ea84f16 100644 --- a/docs/assets/navigation.js +++ b/docs/assets/navigation.js @@ -1 +1 @@ -window.navigationData = "data:application/octet-stream;base64,H4sIAAAAAAAACp2S0UrDMBSG3+Vct85VN7GIN2OCV954N8aITUaDaU5JTsQhvrshuq3pSlq87fm/7+f0ZPMFJD4JSniQmoTRTD1CBi2j2n9rkDsl7Gx3nO2uamqUD7xLzaEsMqhqqbgRGsrNSbU2Bs3ZEuA9q2JRCPV0i6UXKmatp8jyXNrcGwMA31nsX6G2ZFxFE6s6+f+1vspGoKNnfa6jQxs3nTK9iuv7u/miuJS9OBq3+dCoDp+kR9QhZTtmxmXpHXHCejiyGU5YqveMPpiR7E0NnDafx6qbInHQbaeBC8U6v2zvdEXSv5NZGMTS5W0HtEqIdggMgwRIvycdQv9GKRgHObxAtj9bUz5X1wMAAA==" \ No newline at end of file +window.navigationData = "data:application/octet-stream;base64,H4sIAAAAAAAACp2TXWuDMBSG/0uudV3d2jEZu9kHDAbbxe5KKZk5YmhMJDkZk7H/vuBqNVai9NbzPs/rwePmhyB8I0nJHZcIWlJxTyJSUSzcs1IxK8Asdu1sd1FgKVxgzyUjaRKRrOCCaZAk3RxVT1or3VkaOKeZL2pCA91q7YSCGuMoNCzmJnbGBiC/ke9/UNKgthnOrOrlz2t916rkBl75HqYKe9Hzuh4hB62BvVnsurCu/JpealBzeXuzXCU94QcvQVl8kQHdMTNTFn65LjSpU8/cIaIO2drMtCy8o5qxnprYTM1YavAPfFHN6acYuct46auuksCFbHsN7PD1u5Lcygy5u/NFO/PV62sPF7QeZ90gABoBUI2BzSAA4v9FjKGHUQhWo5w6QbZ/624If9MEAAA=" \ No newline at end of file diff --git a/docs/assets/search.js b/docs/assets/search.js index 22784d2..c019562 100644 --- a/docs/assets/search.js +++ b/docs/assets/search.js @@ -1 +1 @@ -window.searchData = "data:application/octet-stream;base64,H4sIAAAAAAAACq2VS4+bMBRG/8vtFiW8H9FolpVmNZuqG4QQCo6KamwETtsI8d97eRqPjJtW3US5+JzvYq5Demj5zw4uaQ/fK1bCJfQtYEVN4AKCgwX3luLX251dRcVZdxb89E3UFFeutOg6gi7AYGn0ktDioUmYrj8b0lFCGk3IdP3ZEFHVhN+FbjfzijHI3XJeKiZIywr6ukXVvLxT0p3zdSk3Z9lJ5AQy8Qt/Y1uWeDRq0riqibOgKVrChHpHpiafK0Tow9RpQf6hXRgEXrg1y/Mx/NlOnzb8Tw2laNzp+27Mmt7v2ln/9QOdT41xdCvy/9qZt7YxzzfMLMB+5BdcevhB2g5/EUi4J++UoHKrCC3HN8N8GxjI63oMyZa1r+QqeDsSM3K2wUpty7VPUeJnmZWuxrQwXZgwBytHhzkK5mLl6jBXwTysPB3mKZiPla/DfAULsAp0WKBgIVahDgsVLMIq0mGRgsVYxTosVjAcSprosER9vOPTdrRzcOZBTHPHgQtSvs3zxwmuP8Qe8uVQROsB7CHCj2GQR2CsxuDlFS8dRzrOgbMewL3mS80/0JY/Aum40nEPnO29Ly1PWp7ZqtjeS6SXmL0PDXES8pHYRyrfKzvjWLitr3TphdILDz11W4FUgkPlw45i6cQ6B49XUzWEVgyZNBuG37P6I81kCAAA"; \ No newline at end of file +window.searchData = "data:application/octet-stream;base64,H4sIAAAAAAAACs1XTY+bMBD9L+6VEobPEFU9VZX2tJeqFxRF0eJoaQEjINtGEf+9Y75sRzabpKnUy2aN35s3M8/Dx5nU7FdDNsmZ/MzKlGxC3yLlvqBkQ1pGLHKsc/z3cCxf2oyVzapl9mtb5Ljzku+bhiKXkM7S0FOa70+aCP31a4M0OaWVJkh//dogbVZQdmx11Qw715d0oHVNU21Vw9ZiKHeO9CkrW1qX+/zzHKtg6TGnzWo3be2WYzlxBIGI+I09lXOs9lSpkfiuJpxFqn1Ny1bNaEnka4aQ/LSkNELukAuDwAtnsd2OB79W6cMMf09QEBcrfZZOjEb7WXtsbm7ocAAXrZsgj5NbLm3GPEDwyzgWi4oS6J8fmkutG46NXIuQB8cVN4iqZkXW3C5vC+Itadhj8oZsatqw/O2ObATx77J5iDVTNmP4j959ac1FGZv1g77ccEhFdiPv/2gVT2bqFNzdqb6kbmsRzI3+JpszeaN1gw865Lq2Z8cY7JDRPOWvDkPSKMWKgoffjnvfMQirOWKArBxiJY7lBnbseNutlUyMfqO/0MMw7QR0MFBgLq5cHcxVYHhcEk8H8xSYjytfB/MVWICrQAcLFFiIq1AHCxVYhKtIB4sU2BpXawt8O4ZIga0VGJqSxLposdpe3m3Q+gAXRvRO6K1QvQDec9C6Aaod4JmKAdUQ4I0HrXOgegK896A1D1RbIDSKq8YANwC0RoPqDRjNgcGdfohwelqaPg3DhOMwzfeZ7MYJW0+zfyZr/NNZBPsx/Ibjb3+9E3PGV1xQvJWKcL4I579D69+MBRNcQUXvTNz+3V5iSSQDZ7rlyLRA0AIDbX4ySmKepGYqb7ovSzRJDV000cZHn8STmgmmNMcPFcGS2mjq4vxdIlhSZd4yKyuVHB0pR2eZeWm47J3JPP4tKCiSmFGLHaYvBcGLBM/U/5aphYWCEhopFxXFghPrODiSVVbRPCsRk2y77g9dW7AQBg8AAA=="; \ No newline at end of file diff --git a/docs/coverage.svg b/docs/coverage.svg index 2f0f210..80b4364 100644 --- a/docs/coverage.svg +++ b/docs/coverage.svg @@ -5,13 +5,13 @@ - - + + document document - 100% - 100% + 78% + 78% \ No newline at end of file diff --git a/docs/functions/deferred.html b/docs/functions/deferred.html new file mode 100644 index 0000000..2b4ab75 --- /dev/null +++ b/docs/functions/deferred.html @@ -0,0 +1,179 @@ +deferred | @mrspartak/promises
  • Creates a deferred promise, allowing manual resolution or rejection.

    +

    Example


    import { deferred } from "@mrspartak/promises"
    import { readStream } from "./stream"

    // Create a deferred promise
    const { promise, resolve, reject } = deferred<void>()

    // Read the stream in chunks and then resolve the promise
    const stream = await readStream()
    let data = ''
    stream.on('data', (chunk) => {
    data += chunk
    })
    stream.on('end', () => {
    resolve()
    })

    // Resolve the promise with the data
    await promise
    console.log(data) // Data is ready +
    +

    Type Parameters

    • T

      The type of the value that the promise resolves to.

      +

    Returns DeferredOut<T>

    An object containing the promise, resolve, and reject functions.

    +
\ No newline at end of file diff --git a/docs/functions/delay.html b/docs/functions/delay.html index 44c0296..71e2d5d 100644 --- a/docs/functions/delay.html +++ b/docs/functions/delay.html @@ -162,7 +162,7 @@

Parameters

  • ms: number

    The number of milliseconds to delay execution.

Returns Promise<void>

A promise that resolves after the specified delay.

Throws

Thrown if the input is not a number or if the number is negative.

-
interface PromiseLike<T> {
    then<TResult1, TResult2>(onfulfilled?, onrejected?): PromiseLike<TResult1 | TResult2>;
}

Type Parameters

  • T

Methods

Methods

  • Attaches callbacks for the resolution and/or rejection of the Promise.

    +

    Type Parameters

    • TResult1 = T
    • TResult2 = never

    Parameters

    Returns PromiseLike<TResult1 | TResult2>

    A Promise for the completion of which ever callback is executed.

    +
\ No newline at end of file diff --git a/docs/modules.html b/docs/modules.html index 254c36c..46c3ee9 100644 --- a/docs/modules.html +++ b/docs/modules.html @@ -157,7 +157,8 @@ --md-sys-color-surface-container-highest: #e4e1e6 }

@mrspartak/promises

Index

Modules

Functions

Functions

deferred +delay sleep timeout to diff --git a/docs/modules/_internal_.html b/docs/modules/_internal_.html index fe16592..9508c77 100644 --- a/docs/modules/_internal_.html +++ b/docs/modules/_internal_.html @@ -158,7 +158,9 @@ }

Index

Interfaces

Type Aliases

Type Aliases

DeferredOut +TimeoutIn TimeoutOut ToFinally ToIn diff --git a/docs/types/_internal_.DeferredOut.html b/docs/types/_internal_.DeferredOut.html new file mode 100644 index 0000000..52f5cf9 --- /dev/null +++ b/docs/types/_internal_.DeferredOut.html @@ -0,0 +1,176 @@ +DeferredOut | @mrspartak/promises
DeferredOut<T>: {
    promise: Promise<T>;
    reject: ((reason?) => void);
    resolve: ((value) => void);
}

Represents a deferred promise, which exposes the resolve and reject functions.

+

Type Parameters

  • T

    The type of the value that the promise resolves to.

    +

Type declaration

  • promise: Promise<T>
  • reject: ((reason?) => void)
      • (reason?): void
      • Parameters

        • Optional reason: any

        Returns void

  • resolve: ((value) => void)
\ No newline at end of file diff --git a/docs/types/_internal_.TimeoutIn.html b/docs/types/_internal_.TimeoutIn.html index a278dc3..de84355 100644 --- a/docs/types/_internal_.TimeoutIn.html +++ b/docs/types/_internal_.TimeoutIn.html @@ -157,7 +157,7 @@ --md-sys-color-surface-container-highest: #e4e1e6 }
TimeoutIn<T>: Promise<T>

Type alias for a promise of type T.

-

Type Parameters

  • T
TimeoutOut<T>: Promise<T>

Return type for the timeout function.

-

Type Parameters

  • T
ToFinally: (() => void | Promise<void>)

Type alias for a function that can optionally be executed after the promise resolves or rejects. It can return void or a Promise that resolves to void.

-

Type declaration

    • (): void | Promise<void>
    • Returns void | Promise<void>

ToIn<T>: Promise<T>

Type alias for a promise of type T.

-

Type Parameters

  • T
ToOut<T>: [Error, null] | [null, T]

Type alias for the output of the to function. It can be either an array with an Error and null, or an array with null and the result of type T.

-

Type Parameters

  • T