UNPKG

4.39 kBMarkdownView Raw
1# Extendable Promise
2
3A promise that can be delayed (extended) via repeated calls to `waitUntil`.
4
5"Extendable" here refers to the ability to extend the duration of the promise, not inheritance/subtypes/etc.
6The name and API are modelled after the
7[`ExtendableEvent`](https://developer.mozilla.org/en-US/docs/Web/API/ExtendableEvent)
8interface that was introduced to the web part of the Service Worker API. As
9such, it provides a `waitUntil` method that takes a promise as argument that
10extends settlement of the parent promise until all promises that were
11added in this way have settled. You can think about it like
12`Promise.allSettled`, except you can add promises to the list after the fact. Example:
13
14```js
15const p = new ExtendablePromise(timeout(200).then(() => 1));
16await timeout(100)
17p.waitUntil(timeout(300).then(() => Promise.reject(Error('2'))));
18
19const res = await p; // total duration ~400ms (100 + 300)
20
21assertEquals(res, [
22 { status: "fulfilled", value: 1 },
23 { status: "rejected", reason: Error('2') },
24]);
25```
26
27After all promises have settled, further calls to `waitUntil` become no-ops.
28
29The constructor takes a promise as argument, which sets the minimum duration for the extendable promise.
30
31If an extendable promise is created without supplying an initial promise, it is "idle" (`await`ing it pauses execution) until the first call to `waitUntil` "primes" it.
32
33Initializing it with a synchronous value or a settled promise immediately settles the extendable promise with that value and all calls to `waitUntil` are no-ops.
34
35--------
36
37<p align="center"><a href="https://workers.tools"><img src="https://workers.tools/assets/img/logo.svg" width="100" height="100" /></a>
38<p align="center">This module is part of the Worker Tools collection<br/>⁕
39
40[Worker Tools](https://workers.tools) are a collection of TypeScript libraries for writing web servers in [Worker Runtimes](https://workers.js.org) such as Cloudflare Workers, Deno Deploy and Service Workers in the browser.
41
42If you liked this module, you might also like:
43
44- 🧭 [__Worker Router__][router] --- Complete routing solution that works across CF Workers, Deno and Service Workers
45- πŸ”‹ [__Worker Middleware__][middleware] --- A suite of standalone HTTP server-side middleware with TypeScript support
46- πŸ“„ [__Worker HTML__][html] --- HTML templating and streaming response library
47- πŸ“¦ [__Storage Area__][kv-storage] --- Key-value store abstraction across [Cloudflare KV][cloudflare-kv-storage], [Deno][deno-kv-storage] and browsers.
48- πŸ†— [__Response Creators__][response-creators] --- Factory functions for responses with pre-filled status and status text
49- 🎏 [__Stream Response__][stream-response] --- Use async generators to build streaming responses for SSE, etc...
50- πŸ₯ [__JSON Fetch__][json-fetch] --- Drop-in replacements for Fetch API classes with first class support for JSON.
51- πŸ¦‘ [__JSON Stream__][json-stream] --- Streaming JSON parser/stingifier with first class support for web streams.
52
53Worker Tools also includes a number of polyfills that help bridge the gap between Worker Runtimes:
54- ✏️ [__HTML Rewriter__][html-rewriter] --- Cloudflare's HTML Rewriter for use in Deno, browsers, etc...
55- πŸ“ [__Location Polyfill__][location-polyfill] --- A `Location` polyfill for Cloudflare Workers.
56- πŸ¦• [__Deno Fetch Event Adapter__][deno-fetch-event-adapter] --- Dispatches global `fetch` events using Deno’s native HTTP server.
57
58[router]: https://workers.tools/router
59[middleware]: https://workers.tools/middleware
60[html]: https://workers.tools/html
61[kv-storage]: https://workers.tools/kv-storage
62[cloudflare-kv-storage]: https://workers.tools/cloudflare-kv-storage
63[deno-kv-storage]: https://workers.tools/deno-kv-storage
64[kv-storage-polyfill]: https://workers.tools/kv-storage-polyfill
65[response-creators]: https://workers.tools/response-creators
66[stream-response]: https://workers.tools/stream-response
67[json-fetch]: https://workers.tools/json-fetch
68[json-stream]: https://workers.tools/json-stream
69[request-cookie-store]: https://workers.tools/request-cookie-store
70[extendable-promise]: https://workers.tools/extendable-promise
71[html-rewriter]: https://workers.tools/html-rewriter
72[location-polyfill]: https://workers.tools/location-polyfill
73[deno-fetch-event-adapter]: https://workers.tools/deno-fetch-event-adapter
74
75Fore more visit [workers.tools](https://workers.tools).