<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Sergio Tskhovrebov</title>
    <description>The latest articles on DEV Community by Sergio Tskhovrebov (@sinxwal).</description>
    <link>https://dev.to/sinxwal</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F370466%2F6055b707-ca31-455c-b031-3da52f36e6b8.jpeg</url>
      <title>DEV Community: Sergio Tskhovrebov</title>
      <link>https://dev.to/sinxwal</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sinxwal"/>
    <language>en</language>
    <item>
      <title>Looking for Promise.any? Let's quickly implement a polyfill for it.</title>
      <dc:creator>Sergio Tskhovrebov</dc:creator>
      <pubDate>Sun, 26 Apr 2020 11:46:01 +0000</pubDate>
      <link>https://dev.to/sinxwal/looking-for-promise-any-let-s-quickly-implement-a-polyfill-for-it-1kga</link>
      <guid>https://dev.to/sinxwal/looking-for-promise-any-let-s-quickly-implement-a-polyfill-for-it-1kga</guid>
      <description>&lt;h2&gt;
  
  
  The Problem
&lt;/h2&gt;

&lt;p&gt;As we all know, we often write asynchronous code using Promise object, which is available since ES6 (ECMAScript 2015). It gracefully offers us several methods.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Promise.resolve&lt;/code&gt; returns a value;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Promise.reject&lt;/code&gt; rejects an error;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Promise.all&lt;/code&gt; waits until list of promises is resolved or rejected;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Promise.race&lt;/code&gt; waits until any of the promises is resolved or rejected.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There is also &lt;code&gt;Promise.any&lt;/code&gt; method &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/any"&gt;(more details)&lt;/a&gt;, that could be very helpful for us. It returns the first resolved promise and stops execution, ignoring the other promises. It is an ECMAScript language proposal and is not supported by browsers yet.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Solution
&lt;/h2&gt;

&lt;p&gt;Fortunately, we can implement such behaviour ourselves:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;promiseAny&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="nx"&gt;iterable&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Iterable&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;T&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nx"&gt;PromiseLike&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;all&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;[...&lt;/span&gt;&lt;span class="nx"&gt;iterable&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;promise&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
        &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;promise&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;})&lt;/span&gt;
  &lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nx"&gt;errors&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;errors&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Some details
&lt;/h2&gt;

&lt;p&gt;Let's dive deeper into the process.&lt;/p&gt;

&lt;p&gt;The main idea is transforming the passed promises list into a reverted promises list. When a reverted Promise resolves it calls &lt;code&gt;reject&lt;/code&gt;, while when it rejects it calls &lt;code&gt;resolve&lt;/code&gt;. Then the reverted promises list is passed to &lt;code&gt;Promise.all&lt;/code&gt; method and when any of Promises rejects, &lt;code&gt;Promise.all&lt;/code&gt; will terminate execution with reject error. &lt;br&gt;
However in reality this means that we have the successful result, so we just transform the result from reject to resolve back and that's all. &lt;br&gt;
We got first successfully resolved promise as a result without magic wand.&lt;/p&gt;

&lt;h2&gt;
  
  
  More details
&lt;/h2&gt;

&lt;p&gt;As a parameter we can pass an Array containing Promises or basic data types (number, String, etc.). To handle basic types we have to &lt;em&gt;promisify&lt;/em&gt; them using &lt;code&gt;Promise.resolve(promise)&lt;/code&gt;.&lt;br&gt;
&lt;code&gt;PromiseLike&lt;/code&gt; is built-in TypeScript data type that wraps and properly handles promises from different libraries that you can use (such as &lt;a href="https://api.jquery.com/category/deferred-object/"&gt;jQuery&lt;/a&gt;, &lt;a href="http://bluebirdjs.com/docs/api-reference.html"&gt;bluebird&lt;/a&gt;, &lt;a href="https://promisesaplus.com/implementations"&gt;Promises/A+&lt;/a&gt;, etc.)&lt;/p&gt;

&lt;p&gt;Another interesting point is the &lt;code&gt;Iterable&lt;/code&gt; type. It's usage means that we can pass in our function not only an Array but also a Map, a Set or even &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function*"&gt;a Generator Function&lt;/a&gt;,  that's to say any object implementing &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterable_protocol"&gt;Iterable protocol&lt;/a&gt;. Our polyfill handles that argument type out of the box using  &lt;code&gt;[...iterable].map&lt;/code&gt; command.&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>typescript</category>
      <category>javascript</category>
      <category>async</category>
    </item>
  </channel>
</rss>
