UNPKG

3.96 kBHTMLView Raw
1<!doctype html>
2<html>
3
4<head>
5 <meta name="generator" content="JSDoc 3.6.2">
6 <meta charset="utf-8">
7 <title>keyu 2.0.0 &raquo; Source: concurrency/index.js</title>
8 <link rel="stylesheet" href="https://brick.a.ssl.fastly.net/Karla:400,400i,700,700i" type="text/css">
9 <link rel="stylesheet" href="https://brick.a.ssl.fastly.net/Noto+Serif:400,400i,700,700i" type="text/css">
10 <link rel="stylesheet" href="https://brick.a.ssl.fastly.net/Inconsolata:500" type="text/css">
11 <link href="css/baseline.css" rel="stylesheet">
12</head>
13
14<body onload="prettyPrint()">
15 <nav id="jsdoc-navbar" role="navigation" class="jsdoc-navbar">
16 <div id="jsdoc-navbar-container">
17 <div id="jsdoc-navbar-content">
18 <a href="index.html" class="jsdoc-navbar-package-name">keyu 2.<wbr>0.<wbr>0</a>
19 </div>
20 </div>
21 </nav>
22 <div id="jsdoc-body-container">
23 <div id="jsdoc-content">
24 <div id="jsdoc-content-container">
25 <div id="jsdoc-banner" role="banner">
26 </div>
27 <div id="jsdoc-main" role="main">
28 <header class="page-header">
29 <h1>Source: concurrency/index.js</h1>
30 </header>
31 <article>
32 <pre class="prettyprint linenums"><code>/** @module concurrency */
33
34/**A **Promise.all** that does not fails-fast.
35 * Given N promises will return all of them &amp;lt;u&gt;independenly if they failed or not&amp;lt;/u&gt;.
36 * @see [Fail-fast](https://en.wikipedia.org/wiki/Fail-fast)
37 * @see [Rob-Pike](https://www.youtube.com/watch?v&#x3D;f6kdp27TYZs)
38 * @see [concurrencyTest.js](https://github.com/nerac/keyu/blob/master/test/concurrencyTest.js)
39 * @argument {Array(Promise)} promises An array of all promises to be executed
40 * @returns {Array(Object)}
41 * @example
42 * await full([Promise.resolve(1), Promise.reject(2), Promise.resolve(3)])
43 * // [ { value: 1 }, { error: 2 }, { value: 3 } ]
44 * @see [concurrencyTest.js](https://github.com/nerac/keyu/blob/master/test/concurrencyTest.js)
45 * @method
46 */
47const full &#x3D; promises &#x3D;&gt; Promise.all(promises.map(promise &#x3D;&gt; promise.then(value &#x3D;&gt; ({ value })).catch(error &#x3D;&gt; ({ error }))));
48
49/** Given **N promises** will return &amp;lt;u&gt;the fastest non-failed one&amp;lt;/u&gt;.
50 * This pattern can be useful some times to reduce latency.
51 * @see [When Do Redundant Requests Reduce Latency?](https://ieeexplore.ieee.org/document/7348681)
52 * @see [Rob-Pike](https://www.youtube.com/watch?v&#x3D;f6kdp27TYZs)
53 * @argument {Array(Promise)} promises An array of all promises to be executed
54 * @returns {Promise}
55 * @example
56 * await best([Promise.resolve(1),Promise.resolve(2)]) // -&gt; 1 (assuming 1 is the first to resolve)
57 * await best([Promise.reject(1),Promise.resolve(2)]) // -&gt; 2
58 * @see [concurrencyTest.js](https://github.com/nerac/keyu/blob/master/test/concurrencyTest.js)
59 * @method
60 */
61const best &#x3D; promises &#x3D;&gt;
62 new Promise((resolve, reject, errors &#x3D; []) &#x3D;&gt; {
63 promises.map(promise &#x3D;&gt;
64 promise.then(resolve).catch(err &#x3D;&gt; {
65 errors.push(err);
66 if (errors.length &#x3D;&#x3D;&#x3D; promises.length) {
67 reject(errors);
68 }
69 })
70 );
71 });
72
73module.exports &#x3D; { best, full };
74</code></pre>
75 </article>
76 </div>
77 </div>
78 <nav id="jsdoc-toc-nav" role="navigation"></nav>
79 </div>
80 </div>
81 <footer id="jsdoc-footer" class="jsdoc-footer">
82 <div id="jsdoc-footer-container">
83 <p>
84 Generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc</a> 3.6.2 on June 17, 2019.
85 </p>
86 </div>
87 </footer>
88 <script src="scripts/jquery.min.js"></script>
89 <script src="scripts/jquery.cookie.js"></script>
90 <script src="scripts/tree.jquery.js"></script>
91 <script src="scripts/prettify.js"></script>
92 <script src="scripts/jsdoc-toc.js"></script>
93 <script src="scripts/linenumber.js"></script>
94 <script src="scripts/scrollanchor.js"></script>
95</body>
96
97</html>
\No newline at end of file