-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.html
223 lines (220 loc) Β· 29.3 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
<!DOCTYPE html><html class="default" lang="en"><head><meta charSet="utf-8"/><meta http-equiv="x-ua-compatible" content="IE=edge"/><title>@mrspartak/promises</title><meta name="description" content="Documentation for @mrspartak/promises"/><meta name="viewport" content="width=device-width, initial-scale=1"/><link rel="stylesheet" href="assets/style.css"/><link rel="stylesheet" href="assets/highlight.css"/><script defer src="assets/main.js"></script><script async src="assets/icons.js" id="tsd-icons-script"></script><script async src="assets/search.js" id="tsd-search-script"></script><script async src="assets/navigation.js" id="tsd-nav-script"></script><style>
@media (prefers-color-scheme: dark) {
:root {
--md-sys-color-primary: #bac3ff;
--md-sys-color-on-primary: #08218a;
--md-sys-color-primary-container: #293ca0;
--md-sys-color-on-primary-container: #dee0ff;
--md-sys-color-secondary: #c3c5dd;
--md-sys-color-on-secondary: #2d2f42;
--md-sys-color-secondary-container: #434659;
--md-sys-color-on-secondary-container: #e0e1f9;
--md-sys-color-tertiary: #e6bad7;
--md-sys-color-on-tertiary: #44263d;
--md-sys-color-tertiary-container: #5d3c55;
--md-sys-color-on-tertiary-container: #ffd7f1;
--md-sys-color-error: #ffb4ab;
--md-sys-color-on-error: #690005;
--md-sys-color-error-container: #93000a;
--md-sys-color-on-error-container: #ffb4ab;
--md-sys-color-background: #1b1b1f;
--md-sys-color-on-background: #e4e1e6;
--md-sys-color-surface: #1b1b1f;
--md-sys-color-on-surface: #e4e1e6;
--md-sys-color-surface-variant: #46464f;
--md-sys-color-on-surface-variant: #c7c5d0;
--md-sys-color-outline: #90909a;
--md-sys-color-outline-variant: #46464f;
--md-sys-color-shadow: #000000;
--md-sys-color-scrim: #000000;
--md-sys-color-inverse-surface: #e4e1e6;
--md-sys-color-inverse-on-surface: #303034;
--md-sys-color-inverse-primary: #4355b9;
--md-sys-color-surface-dim: #131316;
--md-sys-color-surface-bright: #39393c;
--md-sys-color-surface-container-lowest: #0e0e11;
--md-sys-color-surface-container-low: #1b1b1f;
--md-sys-color-surface-container: #1f1f23;
--md-sys-color-surface-container-high: #2a2a2d;
--md-sys-color-surface-container-highest: #353438
}
}
:root[data-theme="dark"] {
--md-sys-color-primary: #bac3ff;
--md-sys-color-on-primary: #08218a;
--md-sys-color-primary-container: #293ca0;
--md-sys-color-on-primary-container: #dee0ff;
--md-sys-color-secondary: #c3c5dd;
--md-sys-color-on-secondary: #2d2f42;
--md-sys-color-secondary-container: #434659;
--md-sys-color-on-secondary-container: #e0e1f9;
--md-sys-color-tertiary: #e6bad7;
--md-sys-color-on-tertiary: #44263d;
--md-sys-color-tertiary-container: #5d3c55;
--md-sys-color-on-tertiary-container: #ffd7f1;
--md-sys-color-error: #ffb4ab;
--md-sys-color-on-error: #690005;
--md-sys-color-error-container: #93000a;
--md-sys-color-on-error-container: #ffb4ab;
--md-sys-color-background: #1b1b1f;
--md-sys-color-on-background: #e4e1e6;
--md-sys-color-surface: #1b1b1f;
--md-sys-color-on-surface: #e4e1e6;
--md-sys-color-surface-variant: #46464f;
--md-sys-color-on-surface-variant: #c7c5d0;
--md-sys-color-outline: #90909a;
--md-sys-color-outline-variant: #46464f;
--md-sys-color-shadow: #000000;
--md-sys-color-scrim: #000000;
--md-sys-color-inverse-surface: #e4e1e6;
--md-sys-color-inverse-on-surface: #303034;
--md-sys-color-inverse-primary: #4355b9;
--md-sys-color-surface-dim: #131316;
--md-sys-color-surface-bright: #39393c;
--md-sys-color-surface-container-lowest: #0e0e11;
--md-sys-color-surface-container-low: #1b1b1f;
--md-sys-color-surface-container: #1f1f23;
--md-sys-color-surface-container-high: #2a2a2d;
--md-sys-color-surface-container-highest: #353438
}
@media (prefers-color-scheme: light) {
:root {
--md-sys-color-primary: #4355b9;
--md-sys-color-on-primary: #ffffff;
--md-sys-color-primary-container: #dee0ff;
--md-sys-color-on-primary-container: #00105c;
--md-sys-color-secondary: #5b5d72;
--md-sys-color-on-secondary: #ffffff;
--md-sys-color-secondary-container: #e0e1f9;
--md-sys-color-on-secondary-container: #181a2c;
--md-sys-color-tertiary: #77536d;
--md-sys-color-on-tertiary: #ffffff;
--md-sys-color-tertiary-container: #ffd7f1;
--md-sys-color-on-tertiary-container: #2d1228;
--md-sys-color-error: #ba1a1a;
--md-sys-color-on-error: #ffffff;
--md-sys-color-error-container: #ffdad6;
--md-sys-color-on-error-container: #410002;
--md-sys-color-background: #fefbff;
--md-sys-color-on-background: #1b1b1f;
--md-sys-color-surface: #fefbff;
--md-sys-color-on-surface: #1b1b1f;
--md-sys-color-surface-variant: #e3e1ec;
--md-sys-color-on-surface-variant: #46464f;
--md-sys-color-outline: #767680;
--md-sys-color-outline-variant: #c7c5d0;
--md-sys-color-shadow: #000000;
--md-sys-color-scrim: #000000;
--md-sys-color-inverse-surface: #303034;
--md-sys-color-inverse-on-surface: #f3f0f4;
--md-sys-color-inverse-primary: #bac3ff;
--md-sys-color-surface-dim: #dcd9de;
--md-sys-color-surface-bright: #fbf8fd;
--md-sys-color-surface-container-lowest: #ffffff;
--md-sys-color-surface-container-low: #f6f2f7;
--md-sys-color-surface-container: #f0edf1;
--md-sys-color-surface-container-high: #eae7ec;
--md-sys-color-surface-container-highest: #e4e1e6
}
}
:root[data-theme="light"] {
--md-sys-color-primary: #4355b9;
--md-sys-color-on-primary: #ffffff;
--md-sys-color-primary-container: #dee0ff;
--md-sys-color-on-primary-container: #00105c;
--md-sys-color-secondary: #5b5d72;
--md-sys-color-on-secondary: #ffffff;
--md-sys-color-secondary-container: #e0e1f9;
--md-sys-color-on-secondary-container: #181a2c;
--md-sys-color-tertiary: #77536d;
--md-sys-color-on-tertiary: #ffffff;
--md-sys-color-tertiary-container: #ffd7f1;
--md-sys-color-on-tertiary-container: #2d1228;
--md-sys-color-error: #ba1a1a;
--md-sys-color-on-error: #ffffff;
--md-sys-color-error-container: #ffdad6;
--md-sys-color-on-error-container: #410002;
--md-sys-color-background: #fefbff;
--md-sys-color-on-background: #1b1b1f;
--md-sys-color-surface: #fefbff;
--md-sys-color-on-surface: #1b1b1f;
--md-sys-color-surface-variant: #e3e1ec;
--md-sys-color-on-surface-variant: #46464f;
--md-sys-color-outline: #767680;
--md-sys-color-outline-variant: #c7c5d0;
--md-sys-color-shadow: #000000;
--md-sys-color-scrim: #000000;
--md-sys-color-inverse-surface: #303034;
--md-sys-color-inverse-on-surface: #f3f0f4;
--md-sys-color-inverse-primary: #bac3ff;
--md-sys-color-surface-dim: #dcd9de;
--md-sys-color-surface-bright: #fbf8fd;
--md-sys-color-surface-container-lowest: #ffffff;
--md-sys-color-surface-container-low: #f6f2f7;
--md-sys-color-surface-container: #f0edf1;
--md-sys-color-surface-container-high: #eae7ec;
--md-sys-color-surface-container-highest: #e4e1e6
}
</style><link rel="stylesheet" href="assets/material-style.css"/></head><body><script>document.documentElement.dataset.theme = localStorage.getItem("tsd-theme") || "os";document.body.style.display="none";setTimeout(() => app?app.showPage():document.body.style.removeProperty("display"),500)</script><header class="tsd-page-toolbar"><div class="tsd-toolbar-contents container"><div class="table-cell" id="tsd-search" data-base="."><div class="field"><label for="tsd-search-field" class="tsd-widget tsd-toolbar-icon search no-caption"><svg width="16" height="16" viewBox="0 0 16 16" fill="none"><use href="assets/icons.svg#icon-search"></use></svg></label><input type="text" id="tsd-search-field" aria-label="Search"/></div><div class="field"><div id="tsd-toolbar-links"></div></div><ul class="results"><li class="state loading">Preparing search index...</li><li class="state failure">The search index is not available</li></ul><a href="index.html" class="title">@mrspartak/promises</a></div><div class="table-cell" id="tsd-widgets"><a href="#" class="tsd-widget tsd-toolbar-icon menu no-caption" data-toggle="menu" aria-label="Menu"><svg width="16" height="16" viewBox="0 0 16 16" fill="none"><use href="assets/icons.svg#icon-menu"></use></svg></a></div></div></header><div class="container container-main"><div class="col-content"><div class="tsd-page-title"><h2>@mrspartak/promises</h2></div><div class="tsd-panel tsd-typography"><p align="center">
<img width="560" src="https://raw.githubusercontent.com/mrspartak/promises/master/assets/logo.svg" alt="@mrspartak/promises logo">
</p>
<br/>
<p align="center">
<a href="https://npmjs.com/package/@mrspartak/promises"><img src="https://img.shields.io/npm/v/@mrspartak/promises.svg" alt="npm package"></a>
<a href="https://npmjs.com/package/@mrspartak/promises"><img src="https://img.shields.io/bundlephobia/min/%40mrspartak/promises.svg" alt="bundle size"></a>
<a href="https://npmjs.com/package/@mrspartak/promises"><img src="https://img.shields.io/npm/dw/%40mrspartak%2Fpromises.svg" alt="downloads"></a>
<a href='https://coveralls.io/github/mrspartak/promises?branch=master'><img src='https://coveralls.io/repos/github/mrspartak/promises/badge.svg?branch=master' alt='Coverage Status' /></a>
</p>
<br/>
<a id="md:π§-typescript-promise-utilities" class="tsd-anchor"></a><h1><a href="#md:π§-typescript-promise-utilities">π§ Typescript promise utilities</a></h1><p>@mrspartak/promises is toolbelt with useful promise utilities that also utilizing a power of Typescript</p>
<a id="md:contents" class="tsd-anchor"></a><h2><a href="#md:contents">Contents</a></h2><ul>
<li><a href="#md:Installation">Installation</a></li>
<li><a href="#md:to---Simplified-Promise-Handling-with-Tuples">to</a></li>
<li><a href="#md:delay---Pause-Execution-for-a-Specified-Time">delay</a></li>
<li><a href="#md:timeout---Timeout-a-Promise">timeout</a></li>
<li><a href="#md:deferred---Create-a-Deferred-Promise">deferred</a></li>
<li><a href="#md:retry---Retry-a-Promise-Returning-Function">retry</a></li>
<li><a href="#md:duration---Measure-the-Time-Taken-for-a-Promise-to-Resolve">duration</a></li>
</ul>
<a id="md:installation" class="tsd-anchor"></a><h2><a href="#md:installation">Installation</a></h2><pre><code class="language-sh"><span class="hl-0"># yarn</span><br/><span class="hl-1">yarn</span><span class="hl-2"> </span><span class="hl-3">add</span><span class="hl-2"> </span><span class="hl-3">@mrspartak/promises</span><br/><span class="hl-0"># npm</span><br/><span class="hl-1">npm</span><span class="hl-2"> </span><span class="hl-3">i</span><span class="hl-2"> </span><span class="hl-3">@mrspartak/promises</span><br/><span class="hl-0"># pnpm</span><br/><span class="hl-1">pnpm</span><span class="hl-2"> </span><span class="hl-3">add</span><span class="hl-2"> </span><span class="hl-3">@mrspartak/promises</span><br/><span class="hl-0"># bun</span><br/><span class="hl-1">bun</span><span class="hl-2"> </span><span class="hl-3">add</span><span class="hl-2"> </span><span class="hl-3">@mrspartak/promises</span>
</code><button>Copy</button></pre>
<a id="md:functions" class="tsd-anchor"></a><h2><a href="#md:functions">Functions</a></h2><a id="md:to---simplified-promise-handling-with-tuples" class="tsd-anchor"></a><h3><a href="#md:to---simplified-promise-handling-with-tuples"><code>to</code> - Simplified Promise Handling with Tuples</a></h3><p>This function helps reduce the verbosity of try/catch blocks by allowing you to handle promise results inline. Inspired by Go's error handling, it returns a tuple with either an error or the result, making your asynchronous code cleaner and more maintainable.</p>
<p><strong>Pros:</strong></p>
<ul>
<li><strong>β¨ Cleaner Code</strong>: Reduces the verbosity and complexity of using multiple try/catch blocks, leading to more readable and maintainable code.</li>
<li><strong>π No Need for Predefined Variables</strong>: Eliminates the need to declare variables before the try/catch block, simplifying variable management and ensuring proper typing.</li>
<li><strong>π Forces a Consistent Error Handling Pattern</strong>: Encourages a unified and specific way of dealing with errors, improving code consistency across the project.</li>
</ul>
<pre><code class="language-ts"><span class="hl-4">import</span><span class="hl-2"> { </span><span class="hl-5">to</span><span class="hl-2"> } </span><span class="hl-4">from</span><span class="hl-2"> </span><span class="hl-3">"@mrspartak/promises"</span><br/><span class="hl-4">import</span><span class="hl-2"> { </span><span class="hl-5">api</span><span class="hl-2"> } </span><span class="hl-4">from</span><span class="hl-2"> </span><span class="hl-3">"./api"</span><br/><br/><span class="hl-0">// Simple tuple destructuring</span><br/><span class="hl-6">const</span><span class="hl-2"> [</span><span class="hl-7">apiError</span><span class="hl-2">, </span><span class="hl-7">user</span><span class="hl-2">] = </span><span class="hl-4">await</span><span class="hl-2"> </span><span class="hl-1">to</span><span class="hl-2">(</span><span class="hl-5">api</span><span class="hl-2">.</span><span class="hl-1">get</span><span class="hl-2">(</span><span class="hl-3">"/me"</span><span class="hl-2">))</span><br/><span class="hl-4">if</span><span class="hl-2"> (</span><span class="hl-5">apiError</span><span class="hl-2">) {</span><br/><span class="hl-2"> </span><span class="hl-0">// Handle error</span><br/><span class="hl-2">}</span><br/><br/><span class="hl-0">// Using finally</span><br/><span class="hl-5">$component</span><span class="hl-2">.</span><span class="hl-5">isLoading</span><span class="hl-2"> = </span><span class="hl-6">true</span><br/><span class="hl-6">const</span><span class="hl-2"> [</span><span class="hl-7">apiError</span><span class="hl-2">, </span><span class="hl-7">status</span><span class="hl-2">] = </span><span class="hl-4">await</span><span class="hl-2"> </span><span class="hl-1">to</span><span class="hl-2">(</span><span class="hl-5">api</span><span class="hl-2">.</span><span class="hl-1">post</span><span class="hl-2">(</span><span class="hl-3">"/me/status"</span><span class="hl-2">, { </span><span class="hl-5">status:</span><span class="hl-2"> </span><span class="hl-3">"online"</span><span class="hl-2"> }), () </span><span class="hl-6">=></span><span class="hl-2"> {</span><br/><span class="hl-2"> </span><span class="hl-5">$component</span><span class="hl-2">.</span><span class="hl-5">isLoading</span><span class="hl-2"> = </span><span class="hl-6">false</span><br/><span class="hl-2">})</span><br/><span class="hl-4">if</span><span class="hl-2"> (</span><span class="hl-5">apiError</span><span class="hl-2">) {</span><br/><span class="hl-2"> </span><span class="hl-0">// Handle error</span><br/><span class="hl-2">}</span>
</code><button>Copy</button></pre>
<a id="md:delay---pause-execution-for-a-specified-time" class="tsd-anchor"></a><h3><a href="#md:delay---pause-execution-for-a-specified-time"><code>delay</code> - Pause Execution for a Specified Time</a></h3><p>The delay function pauses the execution of your code for a specified number of milliseconds. This can be useful in various scenarios, such as waiting for an operation to complete, introducing a delay between retries, or simply pausing execution for debugging purposes.</p>
<p>! The function has alias <code>sleep</code></p>
<pre><code class="language-ts"><span class="hl-4">import</span><span class="hl-2"> { </span><span class="hl-5">delay</span><span class="hl-2">, </span><span class="hl-5">sleep</span><span class="hl-2"> } </span><span class="hl-4">from</span><span class="hl-2"> </span><span class="hl-3">"@mrspartak/promises"</span><br/><span class="hl-4">import</span><span class="hl-2"> { </span><span class="hl-5">parsePage</span><span class="hl-2"> } </span><span class="hl-4">from</span><span class="hl-2"> </span><span class="hl-3">"./parser"</span><br/><br/><span class="hl-4">for</span><span class="hl-2"> (</span><span class="hl-6">let</span><span class="hl-2"> </span><span class="hl-5">i</span><span class="hl-2"> = </span><span class="hl-8">0</span><span class="hl-2">; </span><span class="hl-5">i</span><span class="hl-2"> < </span><span class="hl-8">10</span><span class="hl-2">; </span><span class="hl-5">i</span><span class="hl-2">++) {</span><br/><span class="hl-2"> </span><span class="hl-0">// Parse the page and wait for 1 second before parsing the next page</span><br/><span class="hl-2"> </span><span class="hl-6">const</span><span class="hl-2"> </span><span class="hl-7">pageData</span><span class="hl-2"> = </span><span class="hl-4">await</span><span class="hl-2"> </span><span class="hl-1">parsePage</span><span class="hl-2">(</span><span class="hl-5">i</span><span class="hl-2">)</span><br/><span class="hl-2"> </span><span class="hl-4">await</span><span class="hl-2"> </span><span class="hl-1">delay</span><span class="hl-2">(</span><span class="hl-8">1000</span><span class="hl-2">)</span><br/><span class="hl-2">}</span><br/><br/><span class="hl-0">// You can also use alias sleep instead of delay</span><br/><span class="hl-4">await</span><span class="hl-2"> </span><span class="hl-1">sleep</span><span class="hl-2">(</span><span class="hl-8">1000</span><span class="hl-2">)</span>
</code><button>Copy</button></pre>
<a id="md:timeout---timeout-a-promise" class="tsd-anchor"></a><h3><a href="#md:timeout---timeout-a-promise"><code>timeout</code> - Timeout a Promise</a></h3><p>The timeout function allows you to set a maximum time for a promise to resolve. If the promise does not resolve within the specified time, an error is thrown.</p>
<pre><code class="language-ts"><span class="hl-4">import</span><span class="hl-2"> { </span><span class="hl-5">timeout</span><span class="hl-2"> } </span><span class="hl-4">from</span><span class="hl-2"> </span><span class="hl-3">"@mrspartak/promises"</span><br/><span class="hl-4">import</span><span class="hl-2"> { </span><span class="hl-5">api</span><span class="hl-2"> } </span><span class="hl-4">from</span><span class="hl-2"> </span><span class="hl-3">"./api"</span><br/><br/><span class="hl-0">// Can be used as a race condition</span><br/><span class="hl-6">const</span><span class="hl-2"> [</span><span class="hl-7">error</span><span class="hl-2">, </span><span class="hl-7">user</span><span class="hl-2">] = </span><span class="hl-4">await</span><span class="hl-2"> </span><span class="hl-1">timeout</span><span class="hl-2">(</span><span class="hl-5">api</span><span class="hl-2">.</span><span class="hl-1">getUser</span><span class="hl-2">(), </span><span class="hl-8">1000</span><span class="hl-2">)</span><br/><span class="hl-4">if</span><span class="hl-2"> (</span><span class="hl-5">error</span><span class="hl-2">) {</span><br/><span class="hl-2"> </span><span class="hl-0">// error can be either a timeout error or an error from the api</span><br/><span class="hl-2">}</span>
</code><button>Copy</button></pre>
<a id="md:deferred---create-a-deferred-promise" class="tsd-anchor"></a><h3><a href="#md:deferred---create-a-deferred-promise"><code>deferred</code> - Create a Deferred Promise</a></h3><p>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.</p>
<pre><code class="language-ts"><span class="hl-4">import</span><span class="hl-2"> { </span><span class="hl-5">deferred</span><span class="hl-2"> } </span><span class="hl-4">from</span><span class="hl-2"> </span><span class="hl-3">"@mrspartak/promises"</span><br/><br/><span class="hl-0">// Create a deferred promise</span><br/><span class="hl-6">const</span><span class="hl-2"> { </span><span class="hl-7">promise</span><span class="hl-2">, </span><span class="hl-7">resolve</span><span class="hl-2">, </span><span class="hl-7">reject</span><span class="hl-2"> } = </span><span class="hl-1">deferred</span><span class="hl-2"><</span><span class="hl-9">void</span><span class="hl-2">>()</span><br/><br/><span class="hl-1">setTimeout</span><span class="hl-2">(() </span><span class="hl-6">=></span><span class="hl-2"> {</span><br/><span class="hl-2"> </span><span class="hl-0">// Resolve the promise</span><br/><span class="hl-2"> </span><span class="hl-1">resolve</span><span class="hl-2">()</span><br/><span class="hl-2">}, </span><span class="hl-8">1000</span><span class="hl-2">)</span><br/><br/><span class="hl-4">await</span><span class="hl-2"> </span><span class="hl-5">promise</span><span class="hl-2"> </span><span class="hl-0">// Will wait for 1 second before resolving</span>
</code><button>Copy</button></pre>
<a id="md:retry---retry-a-promise-returning-function" class="tsd-anchor"></a><h3><a href="#md:retry---retry-a-promise-returning-function"><code>retry</code> - Retry a Promise-Returning Function</a></h3><p>The <code>retry</code> function allows you to retry a promise-returning function a specified number of times with an optional delay between attempts if it fails. This can be useful for handling transient errors, such as network requests that may occasionally fail.</p>
<pre><code class="language-ts"><span class="hl-4">import</span><span class="hl-2"> { </span><span class="hl-5">retry</span><span class="hl-2"> } </span><span class="hl-4">from</span><span class="hl-2"> </span><span class="hl-3">"@mrspartak/promises"</span><br/><span class="hl-4">import</span><span class="hl-2"> { </span><span class="hl-5">apiCall</span><span class="hl-2"> } </span><span class="hl-4">from</span><span class="hl-2"> </span><span class="hl-3">"./api"</span><br/><br/><span class="hl-0">// Retry the API call up to 3 times with a delay of 1000 milliseconds between attempts</span><br/><span class="hl-6">const</span><span class="hl-2"> [</span><span class="hl-7">error</span><span class="hl-2">, </span><span class="hl-7">result</span><span class="hl-2">] = </span><span class="hl-4">await</span><span class="hl-2"> </span><span class="hl-1">retry</span><span class="hl-2">(() </span><span class="hl-6">=></span><span class="hl-2"> </span><span class="hl-1">apiCall</span><span class="hl-2">(), </span><span class="hl-8">3</span><span class="hl-2">, { </span><span class="hl-5">delay:</span><span class="hl-2"> </span><span class="hl-8">1000</span><span class="hl-2"> })</span><br/><span class="hl-4">if</span><span class="hl-2"> (</span><span class="hl-5">error</span><span class="hl-2">) {</span><br/><span class="hl-2"> </span><span class="hl-0">// error will always be an error returneb by a promise rejection</span><br/><span class="hl-2">}</span>
</code><button>Copy</button></pre>
<a id="md:duration---measure-the-time-taken-for-a-promise-to-resolve" class="tsd-anchor"></a><h3><a href="#md:duration---measure-the-time-taken-for-a-promise-to-resolve"><code>duration</code> - Measure the Time Taken for a Promise to Resolve</a></h3><p>The <code>duration</code> function allows you to measure the time it takes for a promise to resolve or reject. This is useful for performance monitoring and debugging asynchronous operations in your code.</p>
<pre><code class="language-ts"><span class="hl-4">import</span><span class="hl-2"> { </span><span class="hl-5">duration</span><span class="hl-2"> } </span><span class="hl-4">from</span><span class="hl-2"> </span><span class="hl-3">"@mrspartak/promises"</span><br/><span class="hl-4">import</span><span class="hl-2"> { </span><span class="hl-5">apiCall</span><span class="hl-2"> } </span><span class="hl-4">from</span><span class="hl-2"> </span><span class="hl-3">"./api"</span><br/><br/><span class="hl-0">// Measure the time taken to resolve the API call</span><br/><span class="hl-6">const</span><span class="hl-2"> [</span><span class="hl-7">error</span><span class="hl-2">, </span><span class="hl-7">result</span><span class="hl-2">, </span><span class="hl-7">time</span><span class="hl-2">] = </span><span class="hl-4">await</span><span class="hl-2"> </span><span class="hl-1">duration</span><span class="hl-2">(</span><span class="hl-1">apiCall</span><span class="hl-2">())</span><br/><span class="hl-4">if</span><span class="hl-2"> (</span><span class="hl-5">error</span><span class="hl-2">) {</span><br/><span class="hl-2"> </span><span class="hl-0">// Handle error</span><br/><span class="hl-2">}</span>
</code><button>Copy</button></pre>
</div></div><div class="col-sidebar"><div class="page-menu"><div class="tsd-navigation settings"><details class="tsd-index-accordion"><summary class="tsd-accordion-summary"><h3><svg width="20" height="20" viewBox="0 0 24 24" fill="none"><use href="assets/icons.svg#icon-chevronDown"></use></svg>Settings</h3></summary><div class="tsd-accordion-details"><div class="tsd-filter-visibility"><h4 class="uppercase">Member Visibility</h4><form><ul id="tsd-filter-options"><li class="tsd-filter-item"><label class="tsd-filter-input"><input type="checkbox" id="tsd-filter-protected" name="protected"/><svg width="32" height="32" viewBox="0 0 32 32" aria-hidden="true"><rect class="tsd-checkbox-background" width="30" height="30" x="1" y="1" rx="6" fill="none"></rect><path class="tsd-checkbox-checkmark" d="M8.35422 16.8214L13.2143 21.75L24.6458 10.25" stroke="none" stroke-width="3.5" stroke-linejoin="round" fill="none"></path></svg><span>Protected</span></label></li><li class="tsd-filter-item"><label class="tsd-filter-input"><input type="checkbox" id="tsd-filter-private" name="private"/><svg width="32" height="32" viewBox="0 0 32 32" aria-hidden="true"><rect class="tsd-checkbox-background" width="30" height="30" x="1" y="1" rx="6" fill="none"></rect><path class="tsd-checkbox-checkmark" d="M8.35422 16.8214L13.2143 21.75L24.6458 10.25" stroke="none" stroke-width="3.5" stroke-linejoin="round" fill="none"></path></svg><span>Private</span></label></li><li class="tsd-filter-item"><label class="tsd-filter-input"><input type="checkbox" id="tsd-filter-inherited" name="inherited" checked/><svg width="32" height="32" viewBox="0 0 32 32" aria-hidden="true"><rect class="tsd-checkbox-background" width="30" height="30" x="1" y="1" rx="6" fill="none"></rect><path class="tsd-checkbox-checkmark" d="M8.35422 16.8214L13.2143 21.75L24.6458 10.25" stroke="none" stroke-width="3.5" stroke-linejoin="round" fill="none"></path></svg><span>Inherited</span></label></li><li class="tsd-filter-item"><label class="tsd-filter-input"><input type="checkbox" id="tsd-filter-external" name="external"/><svg width="32" height="32" viewBox="0 0 32 32" aria-hidden="true"><rect class="tsd-checkbox-background" width="30" height="30" x="1" y="1" rx="6" fill="none"></rect><path class="tsd-checkbox-checkmark" d="M8.35422 16.8214L13.2143 21.75L24.6458 10.25" stroke="none" stroke-width="3.5" stroke-linejoin="round" fill="none"></path></svg><span>External</span></label></li></ul></form></div><div class="tsd-theme-toggle"><h4 class="uppercase">Theme</h4><select id="tsd-theme"><option value="os">OS</option><option value="light">Light</option><option value="dark">Dark</option></select></div></div></details></div><details open class="tsd-index-accordion tsd-page-navigation"><summary class="tsd-accordion-summary"><h3><svg width="20" height="20" viewBox="0 0 24 24" fill="none"><use href="assets/icons.svg#icon-chevronDown"></use></svg>On This Page</h3></summary><div class="tsd-accordion-details"><a href="#md:π§-typescript-promise-utilities"><span>π§ <wbr/>Typescript promise utilities</span></a><ul><li><a href="#md:contents"><span>Contents</span></a></li><li><a href="#md:installation"><span>Installation</span></a></li><li><a href="#md:functions"><span>Functions</span></a></li><li><ul><li><a href="#md:to---simplified-promise-handling-with-tuples"><span>to -<wbr/> <wbr/>Simplified <wbr/>Promise <wbr/>Handling with <wbr/>Tuples</span></a></li><li><a href="#md:delay---pause-execution-for-a-specified-time"><span>delay -<wbr/> <wbr/>Pause <wbr/>Execution for a <wbr/>Specified <wbr/>Time</span></a></li><li><a href="#md:timeout---timeout-a-promise"><span>timeout -<wbr/> <wbr/>Timeout a <wbr/>Promise</span></a></li><li><a href="#md:deferred---create-a-deferred-promise"><span>deferred -<wbr/> <wbr/>Create a <wbr/>Deferred <wbr/>Promise</span></a></li><li><a href="#md:retry---retry-a-promise-returning-function"><span>retry -<wbr/> <wbr/>Retry a <wbr/>Promise-<wbr/>Returning <wbr/>Function</span></a></li><li><a href="#md:duration---measure-the-time-taken-for-a-promise-to-resolve"><span>duration -<wbr/> <wbr/>Measure the <wbr/>Time <wbr/>Taken for a <wbr/>Promise to <wbr/>Resolve</span></a></li></ul></li></ul></div></details></div><div class="site-menu"><nav class="tsd-navigation"><a href="modules.html" class="current"><svg class="tsd-kind-icon" viewBox="0 0 24 24"><use href="assets/icons.svg#icon-1"></use></svg><span>@mrspartak/promises</span></a><ul class="tsd-small-nested-navigation" id="tsd-nav-container" data-base="."><li>Loading...</li></ul></nav></div></div></div><footer><p class="tsd-generator">Generated using <a href="https://typedoc.org/" target="_blank">TypeDoc</a></p></footer><div class="overlay"></div><script>
try {
const generateLinkElement = document.querySelector(".tsd-generator a");
const link = document.createElement("a");
Object.assign(link, {
href: "https://github.com/dmnsgn/typedoc-material-theme",
target: "_blank",
rel: "noreferrer",
innerText: "typedoc-material-theme."
});
generateLinkElement.insertAdjacentElement("afterend", link);
generateLinkElement.insertAdjacentText("afterend", " with ");
} catch (error) {
}
</script></body></html>