├── LICENSE
├── README.md
├── index.js
├── package-lock.json
├── package.json
├── test.js
└── types.d.ts
/LICENSE:
--------------------------------------------------------------------------------
1 | The Unlicense
2 |
3 | This is free and unencumbered software released into the public domain.
4 |
5 | Anyone is free to copy, modify, publish, use, compile, sell, or
6 | distribute this software, either in source code form or as a compiled
7 | binary, for any purpose, commercial or non-commercial, and by any
8 | means.
9 |
10 | In jurisdictions that recognize copyright laws, the author or authors
11 | of this software dedicate any and all copyright interest in the
12 | software to the public domain. We make this dedication for the benefit
13 | of the public at large and to the detriment of our heirs and
14 | successors. We intend this dedication to be an overt act of
15 | relinquishment in perpetuity of all present and future rights to this
16 | software under copyright law.
17 |
18 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
22 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 | TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 | SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 |
26 | For more information, please refer to
27 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # race-as-promised
2 |
3 | This module implements `Promise.race()` in a way that does not leak
4 | memory.
5 |
6 | ## Rationale
7 |
8 | The V8 Promise implementation does leak memory in many common
9 | `Promise.race([...])` call cases; see
10 | e.g. https://github.com/nodejs/node/issues/17469.
11 |
12 | The V8 Promise implementation is likely [not going to be
13 | fixed](https://github.com/nodejs/node/issues/17469#issuecomment-349794909).
14 |
15 | See also: https://bugs.chromium.org/p/v8/issues/detail?id=9858
16 |
17 | ## Installation
18 |
19 | ```bash
20 | npm install race-as-promised
21 | ```
22 |
23 | ## Usage
24 |
25 | ```js
26 | const race = require ("race-as-promised");
27 |
28 | // Use race([...]) instead of Promise.race([...])
29 | ```
30 |
31 | ## Author
32 |
33 | The source code and test core [have been made available under The
34 | Unlicense](https://github.com/nodejs/node/issues/17469#issuecomment-776343813)
35 | by [Brian Kim](https://github.com/brainkim), to whom we owe our gratitude.
36 |
37 | An additional issue in the original code has been found and fixed by
38 | [Dan Bornstein](https://github.com/danfuzz), whose efforts are
39 | likewise appreciated.
40 |
41 | ## License
42 |
43 | [The Unlicense](https://spdx.org/licenses/Unlicense.html)
44 |
--------------------------------------------------------------------------------
/index.js:
--------------------------------------------------------------------------------
1 | /*
2 | Authored by Brian Kim:
3 | https://github.com/nodejs/node/issues/17469#issuecomment-685216777
4 |
5 | Adapted to module structure.
6 |
7 | This is free and unencumbered software released into the public domain.
8 |
9 | Anyone is free to copy, modify, publish, use, compile, sell, or
10 | distribute this software, either in source code form or as a compiled
11 | binary, for any purpose, commercial or non-commercial, and by any
12 | means.
13 |
14 | In jurisdictions that recognize copyright laws, the author or authors
15 | of this software dedicate any and all copyright interest in the
16 | software to the public domain. We make this dedication for the benefit
17 | of the public at large and to the detriment of our heirs and
18 | successors. We intend this dedication to be an overt act of
19 | relinquishment in perpetuity of all present and future rights to this
20 | software under copyright law.
21 |
22 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
25 | IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
26 | OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
27 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
28 | OTHER DEALINGS IN THE SOFTWARE.
29 |
30 | For more information, please refer to
31 | */
32 |
33 | function isPrimitive(value) {
34 | return (
35 | value === null ||
36 | (typeof value !== "object" && typeof value !== "function")
37 | );
38 | }
39 |
40 | function addRaceContender(contender) {
41 | const deferreds = new Set();
42 | const record = {deferreds, settled: false};
43 |
44 | // This call to `then` happens once for the lifetime of the value.
45 | Promise.resolve(contender).then(
46 | (value) => {
47 | for (const {resolve} of deferreds) {
48 | resolve(value);
49 | }
50 |
51 | deferreds.clear();
52 | record.settled = true;
53 | },
54 | (err) => {
55 | for (const {reject} of deferreds) {
56 | reject(err);
57 | }
58 |
59 | deferreds.clear();
60 | record.settled = true;
61 | },
62 | );
63 | return record
64 | }
65 |
66 | // Keys are the values passed to race, values are a record of data containing a
67 | // set of deferreds and whether the value has settled.
68 | /** @type {WeakMap