<?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: Gokul N K</title>
    <description>The latest articles on DEV Community by Gokul N K (@nkgokul).</description>
    <link>https://dev.to/nkgokul</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%2F75880%2F3f730a57-b344-4942-b3ce-755d057acaf9.jpeg</url>
      <title>DEV Community: Gokul N K</title>
      <link>https://dev.to/nkgokul</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/nkgokul"/>
    <language>en</language>
    <item>
      <title>JavaScript: Promises or async-await?</title>
      <dc:creator>Gokul N K</dc:creator>
      <pubDate>Tue, 13 Aug 2019 08:31:25 +0000</pubDate>
      <link>https://dev.to/nkgokul/javascript-promises-or-async-await-2a7m</link>
      <guid>https://dev.to/nkgokul/javascript-promises-or-async-await-2a7m</guid>
      <description>

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Ftfxgndou0lobjrx73lih.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Ftfxgndou0lobjrx73lih.png" alt="Promises or Async-Await"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I recently read a medium post where the author claimed that using async-await is better than using promises.&lt;/p&gt;

&lt;p&gt;While this might be true in general cases, I think that generalisation is too broad and doesn't do justice to either async-await or promises.&lt;br&gt;
For someone new to JavaScript, making sense of these and deciding which one to use can be a challenge. In this post, I will list things that I have learned about these and how I decide when to use which.&lt;br&gt;
I read somewhere that async-await is syntactical sugar for using promises. So, before getting to know async-await or deciding which approach to use, make sure that you have a good understanding of promises.&lt;br&gt;
Here are some thumb rules that I follow.&lt;/p&gt;




&lt;h3&gt;
  
  
  Thumb Rules for Using Promises
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fen1fvoa6c6aw17k2iz8z.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fen1fvoa6c6aw17k2iz8z.png" alt="Promises explained"&gt;&lt;/a&gt;&lt;br&gt;
Source : MDN&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Use promises whenever you are using asynchronous or blocking code.
resolve maps to then and reject maps to catch for all practical purposes.&lt;/li&gt;
&lt;li&gt;Make sure to write both &lt;code&gt;.catch&lt;/code&gt; and &lt;code&gt;.then&lt;/code&gt; methods for all the promises.&lt;/li&gt;
&lt;li&gt;If something needs to be done in both cases use &lt;code&gt;.finally&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;We only get one shot at mutating each promise.&lt;/li&gt;
&lt;li&gt;We can add multiple handlers to a single promise.&lt;/li&gt;
&lt;li&gt;The return type of all the methods in the Promise object, regardless of whether they are static methods or prototype methods, is again a Promise.&lt;/li&gt;
&lt;li&gt;In &lt;code&gt;Promise.all&lt;/code&gt;, the order of the promises are maintained in the values variable, irrespective of which promise was first resolved.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Once you have wrapped your head around promises, check out async-await. It helps you to write code that is much more readable. When it is not used properly, it has its downsides.&lt;/p&gt;




&lt;h3&gt;
  
  
  Thumb Rules for async-await
&lt;/h3&gt;

&lt;p&gt;Here's a list of thumb rules that I use to stay sane while using async and await.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;async&lt;/code&gt; functions return a promise.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;async&lt;/code&gt; functions use an implicit Promise to return results. Even if you don't return a promise explicitly, the async function makes sure that your code is passed through a promise.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;await&lt;/code&gt; blocks the code execution within the async function, of which it (await statement) is a part.&lt;/li&gt;
&lt;li&gt;There can be multiple &lt;code&gt;await&lt;/code&gt; statements within a single async function.&lt;/li&gt;
&lt;li&gt;When using &lt;code&gt;async await&lt;/code&gt;, make sure you use try catch for error handling.&lt;/li&gt;
&lt;li&gt;Be extra careful when using await within loops and iterators. You might fall into the trap of writing sequentially-executing code when it could have been easily done in parallel.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;await&lt;/code&gt; is always for a single Promise.&lt;/li&gt;
&lt;li&gt;Promise creation starts the execution of asynchronous functionality.
await only blocks the code execution within the async function. It only makes sure that the next line is executed when the promise resolves. So, if an asynchronous activity has already started, await will not have any effect on it.&lt;/li&gt;
&lt;/ol&gt;




&lt;h3&gt;
  
  
  Should I Use Promises or async-await
&lt;/h3&gt;

&lt;p&gt;The answer is that we will use both.&lt;/p&gt;

&lt;p&gt;Here are the thumb rules that I use to decide when to use promises and when to use async-await.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The async &lt;code&gt;function&lt;/code&gt; returns a promise. The converse is also true. Every function that returns a promise can be considered as async function.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;await&lt;/code&gt; is used for calling an async function and waits for it to resolve or reject.
await blocks the execution of the code within the async function in which it is located.&lt;/li&gt;
&lt;li&gt;If the output of function2 is dependent on the output of function1, I use await.&lt;/li&gt;
&lt;li&gt;If two functions can be run in parallel, create two different async functions and then run them in parallel.&lt;/li&gt;
&lt;li&gt;To run promises in parallel, create an array of promises and then use &lt;code&gt;Promise.all(promisesArray)&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Every time you use &lt;code&gt;await&lt;/code&gt; remember that you are writing blocking code. Over time we tend to neglect this.&lt;/li&gt;
&lt;li&gt;Instead of creating huge async functions with many await &lt;code&gt;asyncFunction()&lt;/code&gt; in it, it is better to create smaller async functions. This way, we will be aware of not writing too much blocking code.&lt;/li&gt;
&lt;li&gt;Another advantage of using smaller &lt;code&gt;async&lt;/code&gt; functions is that you force yourself to think of which &lt;code&gt;async&lt;/code&gt; functions can be run in parallel.&lt;/li&gt;
&lt;li&gt;If your code contains blocking code, it is better to make it an async function. By doing this, you are making sure that somebody else can use your function asynchronously.&lt;/li&gt;
&lt;li&gt;By making async functions out of blocking code, you are enabling the user (who will call your function) to decide on the level of asynchronicity they want.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I hope this helps you decide when to use promises and when to use async-await.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>async</category>
      <category>await</category>
    </item>
    <item>
      <title>Understanding promises in JavaScript</title>
      <dc:creator>Gokul N K</dc:creator>
      <pubDate>Wed, 15 May 2019 17:59:29 +0000</pubDate>
      <link>https://dev.to/nkgokul/understanding-promises-in-javascript-239a</link>
      <guid>https://dev.to/nkgokul/understanding-promises-in-javascript-239a</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--a4InINhw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2560/1%2AIXaKMoKxyvrZs1prvukJvw.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--a4InINhw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2560/1%2AIXaKMoKxyvrZs1prvukJvw.jpeg" alt=""&gt;&lt;/a&gt;&lt;br&gt;
&lt;span class="figcaption_hack"&gt;I am making you a pinky promise that by the end of this post you will know&lt;br&gt;
JavaScript Promises better.&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;I have had a kind of “love and hate” relationship with JavaScript. But&lt;br&gt;
nevertheless JavaScript was always intriguing for me. Having worked on Java and&lt;br&gt;
PHP for the last 10 years, JavaScript seemed very different but intriguing. I&lt;br&gt;
did not get to spend enough time on JavaScript and have been trying to make up&lt;br&gt;
for it of late.&lt;/p&gt;

&lt;p&gt;Promises was the first interesting topic that I came across. Time and again I&lt;br&gt;
have heard people saying that Promises saves you from Callback hell. While that&lt;br&gt;
might have been a pleasant side-effect, there is more to Promises and here is&lt;br&gt;
what I have been able to figure out till now. This is going to be a long&lt;br&gt;
article, if you would like highlight some parts you can use our extension&lt;br&gt;
&lt;a href="http://bit.ly/highlights-extension"&gt;http://bit.ly/highlights-extension&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Background&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When you start working on JavaScript for the first time it can be a little&lt;br&gt;
frustrating. You will hear some people say that JavaScript is synchronous&lt;br&gt;
programming language while others claim that it is asynchronous. You hear&lt;br&gt;
blocking code, non blocking code, event driven design pattern, event life cycle,&lt;br&gt;
function stack, event queue, bubbling, polyfill, babel, angular, reactJS, vue JS&lt;br&gt;
and a ton of other tools and libraries. Fret not. You are not the first. There&lt;br&gt;
is a term for that as well. It is called &lt;strong&gt;JavaScript Fatigue&lt;/strong&gt;. This tweet&lt;br&gt;
captures it very well.&lt;/p&gt;

&lt;p&gt;If you want further details about JavaScript fatigue you should check out the&lt;br&gt;
following article. There is a reason this post got 42k claps on Hackernoon :)&lt;/p&gt;

&lt;p&gt;JavaScript is a synchronous programming language. But thanks to callback&lt;br&gt;
functions we can make it function like Asynchronous programming language.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Promises for layman&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Promises in JavaScript are very similar to the promises you make in real life.&lt;br&gt;
So first let us look at promises in real life.&lt;/p&gt;

&lt;p&gt;The definition of a promise from the dictionary is as follows&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;promise&lt;/strong&gt; : noun : Assurance that one will do something or that a particular&lt;br&gt;
thing will happen.&lt;/p&gt;

&lt;p&gt;So what happens when somebody makes a promise to you ?&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; A promise gives you an assurance that something will be done. Whether they(who
made the promise) will do it themselves or they get it done by others is
immaterial. They give you an assurance based on which you can plan something.&lt;/li&gt;
&lt;li&gt; A promise can either be kept or broken.&lt;/li&gt;
&lt;li&gt; When a promise is kept you expect something out of that promise. You can make
use of the output of a promise for your further actions or plans.&lt;/li&gt;
&lt;li&gt; When a promise is broken, you would like to know why the person who made the
promise was not able to keep up his side of the bargain. Once you know the
reason and have a confirmation that the promise has been broken, you can plan
what to do next or how to handle it.&lt;/li&gt;
&lt;li&gt; At the time of making a promise, all we have is only an assurance. We will not
be able to act on it immediately. We can decide and formulate what needs to be
done when the &lt;strong&gt;promise is kept&lt;/strong&gt; (and hence we have expected outcome) or
&lt;strong&gt;broken&lt;/strong&gt; (we know the reason and hence we can plan a contingency).&lt;/li&gt;
&lt;li&gt; There is a chance that you may not hear back from the person who made the
promise at all. In such cases you would prefer to keep a time threshold. Say if
the person who made the promise doesn’t come back to me in 10 days I will
consider that he had some issues and will not keep up his promise. So even if
the person comes back to you after 15 days it doesn’t matter to you any more as
you have already made alternate plans.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Promises in JavaScript
&lt;/h3&gt;

&lt;p&gt;As a rule of thumb, for JavaScript I always read documentation from MDN Web&lt;br&gt;
Docs. Of all the resources I think they provide the most concise details. I read&lt;br&gt;
up the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise"&gt;Promises page form MDSN Web&lt;br&gt;
Docs&lt;/a&gt;&lt;br&gt;
and played around with code to get a hang of it.&lt;/p&gt;

&lt;p&gt;There are two parts to understanding promises. &lt;strong&gt;Creation of promises&lt;/strong&gt; and&lt;br&gt;
&lt;strong&gt;Handling of promises&lt;/strong&gt;. Though most of our code will generally cater to&lt;br&gt;
handling of promises created by other libraries, a complete understanding will&lt;br&gt;
help us for sure. Understanding of “creation of promises” is equally important&lt;br&gt;
once you cross the beginner stage.&lt;/p&gt;

&lt;h3&gt;
  
  
  Creation of Promises
&lt;/h3&gt;

&lt;p&gt;Let us look at the signature for creating a new promise.&lt;/p&gt;

&lt;p&gt;The constructor accepts a function called executor. This &lt;code&gt;executor&lt;/code&gt; function&lt;br&gt;
accepts two parameters &lt;code&gt;resolve&lt;/code&gt; and &lt;code&gt;reject&lt;/code&gt; which are in turn functions.&lt;br&gt;
Promises are generally used for easier handling of asynchronous operations or&lt;br&gt;
blocking code, examples for which being file operations, API calls, DB calls, IO&lt;br&gt;
calls etc.The initiation of these asynchronous operations happens within the&lt;br&gt;
&lt;code&gt;executor&lt;/code&gt;function. If the asynchronous operations are successful then the&lt;br&gt;
expected result is returned by calling the &lt;code&gt;resolve&lt;/code&gt;function by the creator of&lt;br&gt;
the promise. Similarly if there was some unexpected error the reasons is passed&lt;br&gt;
on by calling the &lt;code&gt;reject&lt;/code&gt;function.&lt;/p&gt;

&lt;p&gt;Now that we know how to create a promise. Let us create a simple promise for our&lt;br&gt;
understanding sake.&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var keepsHisWord;
keepsHisWord = true;
promise1 = new Promise(function(resolve, reject) {
  if (keepsHisWord) {
    resolve("The man likes to keep his word");
  } else {
    reject("The man doesnt want to keep his word");
  }
});
console.log(promise1);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--wMfbRwrq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/800/1%2AByDQ6-tTp2TvHIh4R_P6xw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--wMfbRwrq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/800/1%2AByDQ6-tTp2TvHIh4R_P6xw.png" alt=""&gt;&lt;/a&gt;&lt;br&gt;
&lt;span class="figcaption_hack"&gt;Every promise has a state and value&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;Since this promise gets resolved right away we will not be able to inspect the&lt;br&gt;
initial state of the promise. So let us just create a new promise that will take&lt;br&gt;
some time to resolve. The easiest way for that is to use the &lt;code&gt;setTimeOut&lt;/code&gt;&lt;br&gt;
function.&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;promise2 = new Promise(function(resolve, reject) {
  setTimeout(function() {
    resolve({
      message: "The man likes to keep his word",
      code: "aManKeepsHisWord"
    });
  }, 10 * 1000);
});
console.log(promise2);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The above code just creates a promise that will resolve unconditionally after 10&lt;br&gt;
seconds. So we can checkout the state of the promise until it is resolved.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--rNxu6ZTh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/800/1%2A6GX_rHpGa3HXcYiHm9Vy6g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--rNxu6ZTh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/800/1%2A6GX_rHpGa3HXcYiHm9Vy6g.png" alt=""&gt;&lt;/a&gt;&lt;br&gt;
&lt;span class="figcaption_hack"&gt;state of promise until it is resolved or rejected&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;Once the ten seconds are over the promise is resolved. Both &lt;code&gt;PromiseStatus&lt;/code&gt; and&lt;br&gt;
&lt;code&gt;PromiseValue&lt;/code&gt; are updated accordingly. As you can see we updated the resolve&lt;br&gt;
function so that we can pass a JSON Object instead of a simple string. This is&lt;br&gt;
just to show that we can pass other values as well in the &lt;code&gt;resolve&lt;/code&gt; function.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--s7Hjdxh8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/800/1%2An6s4IswZBVUIHc2K3apONA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--s7Hjdxh8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/800/1%2An6s4IswZBVUIHc2K3apONA.png" alt=""&gt;&lt;/a&gt;&lt;br&gt;
&lt;span class="figcaption_hack"&gt;A promise that resolves after 10 seconds with a JSON object as returned value&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;Now let us look at a promise that will reject. Let us just modify the promise 1&lt;br&gt;
a little for this.&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;keepsHisWord = false;
promise3 = new Promise(function(resolve, reject) {
  if (keepsHisWord) {
    resolve("The man likes to keep his word");
  } else {
    reject("The man doesn't want to keep his word");
  }
});
console.log(promise3);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Since this will create a unhanded rejection chrome browser will show an error.&lt;br&gt;
You can ignore it for now. We will get back to that later.&lt;/p&gt;

&lt;p&gt;&lt;span class="figcaption_hack"&gt;rejections in promises&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;As we can see &lt;code&gt;PromiseStatus&lt;/code&gt; can have three different values. &lt;code&gt;pending&lt;/code&gt;&lt;br&gt;
&lt;code&gt;resolved&lt;/code&gt; or &lt;code&gt;rejected&lt;/code&gt; When promise is created &lt;code&gt;PromiseStatus&lt;/code&gt;will be in the&lt;br&gt;
&lt;code&gt;pending&lt;/code&gt; status and will have &lt;code&gt;PromiseValue&lt;/code&gt; as &lt;code&gt;undefined&lt;/code&gt; until the promise&lt;br&gt;
is either &lt;code&gt;resolved&lt;/code&gt; or &lt;code&gt;rejected.&lt;/code&gt; When a promise is in &lt;code&gt;resolved&lt;/code&gt; or&lt;br&gt;
&lt;code&gt;rejected&lt;/code&gt; states, a promise is said to be &lt;code&gt;settled.&lt;/code&gt; So a promise generally&lt;br&gt;
transitions from pending state to settled state.&lt;/p&gt;

&lt;p&gt;Now that we know how promises are created we can look at how we can use or&lt;br&gt;
handle promises. This will go hand in hand with understanding the &lt;code&gt;Promise&lt;/code&gt;&lt;br&gt;
object.&lt;/p&gt;

&lt;h3&gt;
  
  
  Understanding promises Object
&lt;/h3&gt;

&lt;p&gt;As per MDN documentation&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The &lt;code&gt;Promise&lt;/code&gt; object represents the eventual completion (or failure) of an&lt;br&gt;
asynchronous operation, and its resulting value.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;code&gt;Promise&lt;/code&gt; object has static methods and &lt;code&gt;prototype methods&lt;/code&gt;Static methods in&lt;br&gt;
&lt;code&gt;Promise&lt;/code&gt; object can be applied independently, whereas the &lt;code&gt;prototype methods&lt;/code&gt;&lt;br&gt;
needs to be applied on the instances of &lt;code&gt;Promise&lt;/code&gt; object. Remembering that both&lt;br&gt;
normal methods and prototypes all return a &lt;code&gt;Promise&lt;/code&gt; makes it much easier to&lt;br&gt;
make sense of things.&lt;/p&gt;

&lt;h3&gt;
  
  
  Prototype Methods
&lt;/h3&gt;

&lt;p&gt;Let us first start with the &lt;code&gt;prototype methods&lt;/code&gt; There are three of them. Just to&lt;br&gt;
reiterate remember that all these methods can be applied on an instance of&lt;br&gt;
&lt;code&gt;Promise&lt;/code&gt; object and all these methods return a promise in turn. All the&lt;br&gt;
following methods assigns handlers for different state transitions of a promise.&lt;br&gt;
As we saw earlier when a &lt;code&gt;Promise&lt;/code&gt; is created it is in &lt;code&gt;pending&lt;/code&gt; state. One or&lt;br&gt;
more of the following three methods will be run when a promise is settled based&lt;br&gt;
on whether they are &lt;code&gt;fulfilled&lt;/code&gt; or &lt;code&gt;rejected&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Promise.prototype.catch(onRejected)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Promise.prototype.then(onFulfilled, onRejected)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Promise.prototype.finally(onFinally)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The below image shows the flow for &lt;code&gt;.then&lt;/code&gt; and &lt;code&gt;.catch&lt;/code&gt; methods. Since they&lt;br&gt;
return a &lt;code&gt;Promise&lt;/code&gt; they can be chained again which is also shown in the image.&lt;br&gt;
If &lt;code&gt;.finally&lt;/code&gt; is declared for a promise then it will be executed whenever a&lt;br&gt;
promise is &lt;code&gt;settled&lt;/code&gt; irrespective of whether it is fulfilled or rejected. As&lt;br&gt;
&lt;a href="https://medium.com/@konrud5"&gt;Konstantin Rouda&lt;/a&gt; pointed out there is limited&lt;br&gt;
support for finally, so please check before you use this.&lt;/p&gt;

&lt;p&gt;&lt;span class="figcaption_hack"&gt;From :&lt;br&gt;
&lt;a href="https://mdn.mozillademos.org/files/15911/promises.png"&gt;https://mdn.mozillademos.org/files/15911/promises.png&lt;/a&gt;&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;Here is a small story. You are a school going kid and you ask your mom for a&lt;br&gt;
phone. She says “I will buy a phone for this month end.”&lt;/p&gt;

&lt;p&gt;Let us look at how it will look in JavaScript if the promise gets executed at&lt;br&gt;
the end of the month.&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var momsPromise = new Promise(function(resolve, reject) {
  momsSavings = 20000;
  priceOfPhone = 60000;
  if (momsSavings &amp;gt; priceOfPhone) {
    resolve({
      brand: "iphone",
      model: "6s"
    });
  } else {
    reject("We donot have enough savings. Let us save some more money.");
  }
});

momsPromise.then(function(value) {
  console.log("Hurray I got this phone as a gift ", JSON.stringify(value));
});

momsPromise.catch(function(reason) {
  console.log("Mom coudn't buy me the phone because ", reason);
});

momsPromise.finally(function() {
  console.log(
    "Irrespecitve of whether my mom can buy me a phone or not, I still love her"
  );
});
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The output for this will be.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--msXl1RsI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/800/1%2AOl16n8YEu5vKgVqh0pBbhg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--msXl1RsI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/800/1%2AOl16n8YEu5vKgVqh0pBbhg.png" alt=""&gt;&lt;/a&gt;&lt;br&gt;
&lt;span class="figcaption_hack"&gt;moms failed promise.&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;If we change the value of &lt;code&gt;momsSavings&lt;/code&gt; to 200000 then mom will be able to gift&lt;br&gt;
the son. In such case the output will be&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--tSR5FoUE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/800/1%2A9PPksLxayz3373AxoYg4kA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--tSR5FoUE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/800/1%2A9PPksLxayz3373AxoYg4kA.png" alt=""&gt;&lt;/a&gt;&lt;br&gt;
&lt;span class="figcaption_hack"&gt;mom keeps her promise.&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;Let us wear the hat of somebody who consumes this library. We are mocking the&lt;br&gt;
output and nature so that we can look at how to use then and catch effectively.&lt;/p&gt;

&lt;p&gt;Since &lt;code&gt;.then&lt;/code&gt; can assign both&lt;code&gt;onFulfilled, onRejected handlers&lt;/code&gt; , instead of&lt;br&gt;
writing separate &lt;code&gt;.then&lt;/code&gt; and &lt;code&gt;.catch&lt;/code&gt; we could have done the same with with&lt;br&gt;
&lt;code&gt;.then&lt;/code&gt; It would have looked like below.&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;momsPromise.then(
  function(value) {
    console.log("Hurray I got this phone as a gift ", JSON.stringify(value));
  },
  function(reason) {
    console.log("Mom coudn't buy me the phone because ", reason);
  }
);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;But for readability of the code I think it is better to keep them separate.&lt;/p&gt;

&lt;p&gt;To make sure that we can run all these samples in browsers in general or chrome&lt;br&gt;
in specific I am making sure that we do not have external dependencies in our&lt;br&gt;
code samples. To better understand the further topics let us create a function&lt;br&gt;
that will return a promise which will be resolved or rejected randomly so that&lt;br&gt;
we can test out various scenarios. To understand the concept of asynchronous&lt;br&gt;
functions let us introduce a random delay also into our function. Since we will&lt;br&gt;
need random numbers let us first create a random function that will return a&lt;br&gt;
random number between x and y.&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function getRandomNumber(start = 1, end = 10) {
  //works when both start,end are &amp;gt;=1 and end &amp;gt; start
  return parseInt(Math.random() * end) % (end-start+1) + start;
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Let us create a function that will return a promise for us. Let us call for our&lt;br&gt;
function &lt;code&gt;promiseTRRARNOSG&lt;/code&gt; which is an alias for&lt;br&gt;
&lt;code&gt;promiseThatResolvesRandomlyAfterRandomNumnberOfSecondsGenerator&lt;/code&gt;. This function&lt;br&gt;
will create a promise which will resolve or reject after a random number of&lt;br&gt;
seconds between 2 and 10. To randomise rejection and resolving we will create a&lt;br&gt;
random number between 1 and 10. If the random number generated is greater 5 we&lt;br&gt;
will resolve the promise, else we will reject it.&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function getRandomNumber(start = 1, end = 10) {
  //works when both start and end are &amp;gt;=1
  return (parseInt(Math.random() * end) % (end - start + 1)) + start;
}

var promiseTRRARNOSG = (
 = function() {
  return new Promise(function(resolve, reject) {
    let randomNumberOfSeconds = getRandomNumber(2, 10);
    setTimeout(function() {
      let randomiseResolving = getRandomNumber(1, 10);
      if (randomiseResolving &amp;gt; 5) {
        resolve({
          randomNumberOfSeconds: randomNumberOfSeconds,
          randomiseResolving: randomiseResolving
        });
      } else {
        reject({
          randomNumberOfSeconds: randomNumberOfSeconds,
          randomiseResolving: randomiseResolving
        });
      }
    }, randomNumberOfSeconds * 1000);
  });
});

var testProimse = promiseTRRARNOSG();
testProimse.then(function(value) {
  console.log("Value when promise is resolved : ", value);
});
testProimse.catch(function(reason) {
  console.log("Reason when promise is rejected : ", reason);
});

// Let us loop through and create ten different promises using the function to see some variation. Some will be resolved and some will be rejected. 

for (i=1; i&amp;lt;=10; i++) {
  let promise = promiseTRRARNOSG();
  promise.then(function(value) {
    console.log("Value when promise is resolved : ", value);
  });
  promise.catch(function(reason) {
    console.log("Reason when promise is rejected : ", reason);
  });
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Refresh the browser page and run the code in console to see the different&lt;br&gt;
outputs for &lt;code&gt;resolve&lt;/code&gt; and &lt;code&gt;reject&lt;/code&gt; scenarios. Going forward we will see how we&lt;br&gt;
can create multiple promises and check their outputs without having to do this.&lt;/p&gt;

&lt;h3&gt;
  
  
  Static Methods
&lt;/h3&gt;

&lt;p&gt;There are four static methods in &lt;code&gt;Promise&lt;/code&gt; object.&lt;/p&gt;

&lt;p&gt;The first two are helpers methods or shortcuts. They help you create resolved or&lt;br&gt;
rejected promises easily.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Promise.reject(reason)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Helps you create a rejected promise.&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var promise3 = Promise.reject("Not interested");
promise3.then(function(value){
  console.log("This will not run as it is a resolved promise. The resolved value is ", value);
});
promise3.catch(function(reason){
  console.log("This run as it is a rejected promise. The reason is ", reason);
});
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&lt;code&gt;Promise.resolve(value)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Helps you create a resolved promise.&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var promise4 = Promise.resolve(1);
promise4.then(function(value){
  console.log("This will run as it is a resovled promise. The resolved value is ", value);
});
promise4.catch(function(reason){
  console.log("This will not run as it is a resolved promise", reason);
});
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;On a sidenote a promise can have multiple handlers. So you can update the above&lt;br&gt;
code to&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var promise4 = Promise.resolve(1);
promise4.then(function(value){
  console.log("This will run as it is a resovled promise. The resolved value is ", value);
});
promise4.then(function(value){
  console.log("This will also run as multiple handlers can be added. Printing twice the resolved value which is ", value * 2);
});
promise4.catch(function(reason){
  console.log("This will not run as it is a resolved promise", reason);
});
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;And the output will look like.&lt;/p&gt;

&lt;p&gt;The next two methods helps you process a set of promises. When you are dealing&lt;br&gt;
with multiple promises it is better to create an array of promises first and&lt;br&gt;
then do the necessary action over the set of promises. For understanding these&lt;br&gt;
methods we will not be able to use our handy &lt;code&gt;promiseTRRARNOSG&lt;/code&gt; as it is too&lt;br&gt;
random. It is better to have some deterministic promises so that we can&lt;br&gt;
understand the behaviour. Let us create two functions. One that will resolve&lt;br&gt;
after n seconds and one that will reject after n seconds.&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var promiseTRSANSG = (promiseThatResolvesAfterNSecondsGenerator = function(
  n = 0
) {
  return new Promise(function(resolve, reject) {
    setTimeout(function() {
      resolve({
        resolvedAfterNSeconds: n
      });
    }, n * 1000);
  });
});
var promiseTRJANSG = (promiseThatRejectsAfterNSecondsGenerator = function(
  n = 0
) {
  return new Promise(function(resolve, reject) {
    setTimeout(function() {
      reject({
        rejectedAfterNSeconds: n
      });
    }, n * 1000);
  });
});
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Now let us use these helper functions to understand &lt;code&gt;Promise.All&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Promise.All
&lt;/h3&gt;

&lt;p&gt;As per MDN documentation&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The &lt;code&gt;Promise.all(iterable)&lt;/code&gt; method returns a single &lt;code&gt;Promise&lt;/code&gt; that resolves when&lt;br&gt;
all of the promises in the &lt;code&gt;iterable&lt;/code&gt; argument have resolved or when the&lt;br&gt;
iterable argument contains no promises. It rejects with the reason of the first&lt;br&gt;
promise that rejects.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Case 1&lt;/strong&gt; : When all the promises are resolved. This is the most frequently&lt;br&gt;
used scenario.&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.time("Promise.All");
var promisesArray = [];
promisesArray.push(promiseTRSANSG(1));
promisesArray.push(promiseTRSANSG(4));
promisesArray.push(promiseTRSANSG(2));
var handleAllPromises = Promise.all(promisesArray);
handleAllPromises.then(function(values) {
  console.timeEnd("Promise.All");
  console.log("All the promises are resolved", values);
});
handleAllPromises.catch(function(reason) {
  console.log("One of the promises failed with the following reason", reason);
});
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&lt;span class="figcaption_hack"&gt;All promises resolved.&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;There are two important observations we need to make in general from the output.&lt;/p&gt;

&lt;p&gt;First** : **The third promise which takes 2 seconds finishes before the second&lt;br&gt;
promise which takes 4 seconds. But as you can see in the output, the order of&lt;br&gt;
the promises are maintained in the values.&lt;/p&gt;

&lt;p&gt;Second** : **I added a console timer to find out how long &lt;code&gt;Promise.All&lt;/code&gt; takes.&lt;br&gt;
If the promises were executed in sequential it should have taken 1+4+2=7 seconds&lt;br&gt;
in total. But from our timer we saw that it only takes 4 seconds. This is a&lt;br&gt;
proof that all the promises were executed in parallel.&lt;/p&gt;

&lt;p&gt;**Case 2 : **When there are no promises. I think this is the least frequently&lt;br&gt;
used.&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.time("Promise.All");
var promisesArray = [];
promisesArray.push(1);
promisesArray.push(4);
promisesArray.push(2);
var handleAllPromises = Promise.all(promisesArray);
handleAllPromises.then(function(values) {
  console.timeEnd("Promise.All");
  console.log("All the promises are resolved", values);
});
handleAllPromises.catch(function(reason) {
  console.log("One of the promises failed with the following reason", reason);
});
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Since there are no promises in the array the returning promise is resolved.&lt;/p&gt;

&lt;p&gt;**Case 3 : **It rejects with the reason of the first promise that rejects.&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.time("Promise.All");
var promisesArray = [];
promisesArray.push(promiseTRSANSG(1));
promisesArray.push(promiseTRSANSG(5));
promisesArray.push(promiseTRSANSG(3));
promisesArray.push(promiseTRSANSG(4));
var handleAllPromises = Promise.all(promisesArray);
handleAllPromises.then(function(values) {
  console.timeEnd("Promise.All");
  console.log("All the promises are resolved", values);
});
handleAllPromises.catch(function(reason) {
  console.timeEnd("Promise.All");
  console.log("One of the promises failed with the following reason ", reason);
});
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&lt;span class="figcaption_hack"&gt;Execution stopped after the first rejection&lt;/span&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Promise.race
&lt;/h3&gt;

&lt;p&gt;As per MDN documention&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The &lt;code&gt;Promise.race(iterable)&lt;/code&gt; method returns a promise that resolves or rejects&lt;br&gt;
as soon as one of the promises in the iterable resolves or rejects, with the&lt;br&gt;
value or reason from that promise.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Case 1 :&lt;/strong&gt; One of the promises resolves first.&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.time("Promise.race");
var promisesArray = [];
promisesArray.push(promiseTRSANSG(4));
promisesArray.push(promiseTRSANSG(3));
promisesArray.push(promiseTRJANSG(3));
promisesArray.push(promiseTRSANSG(4));
var promisesRace = Promise.race(promisesArray);
promisesRace.then(function(values) {
  console.timeEnd("Promise.race");
  console.log("The fasted promise resolved", values);
});
promisesRace.catch(function(reason) {
  console.timeEnd("Promise.race");
  console.log("The fastest promise rejected with the following reason ", reason);
});
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--5G_Nc1Tn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/800/1%2A6za2DUGOKIxARTyfP5HihQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--5G_Nc1Tn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/800/1%2A6za2DUGOKIxARTyfP5HihQ.png" alt=""&gt;&lt;/a&gt;&lt;br&gt;
&lt;span class="figcaption_hack"&gt;fastest resolution&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;All the promises are run in parallel. The third promise resolves in 2 seconds.&lt;br&gt;
As soon as this is done the promise returned by &lt;code&gt;Promise.race&lt;/code&gt; is resolved.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Case 2:&lt;/strong&gt; One of the promises rejects first.&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.time("Promise.race");
var promisesArray = [];
promisesArray.push(promiseTRSANSG(4));
promisesArray.push(promiseTRSANSG(6));
promisesArray.push(promiseTRSANSG(5));
promisesArray.push(promiseTRSANSG(4));
var promisesRace = Promise.race(promisesArray);
promisesRace.then(function(values) {
  console.timeEnd("Promise.race");
  console.log("The fasted promise resolved", values);
});
promisesRace.catch(function(reason) {
  console.timeEnd("Promise.race");
  console.log("The fastest promise rejected with the following reason ", reason);
});
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&lt;span class="figcaption_hack"&gt;fastest rejection&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;All the promises are run in parallel. The fourth promise rejected in 3 seconds.&lt;br&gt;
As soon as this is done the promise returned by &lt;code&gt;Promise.race&lt;/code&gt; is rejected.&lt;/p&gt;

&lt;p&gt;I have written all the example methods so that I can test out various scenarios&lt;br&gt;
and tests can be run in the browser itself. That is the reason you don’t see any&lt;br&gt;
API calls, file operations or database calls in the examples. While all of these&lt;br&gt;
are real life example you need additional effort to set them up and test it.&lt;br&gt;
Whereas using the delay functions gives you similar scenarios without the burden&lt;br&gt;
of additional setup. You can easily play around with the values to see and&lt;br&gt;
checkout different scenarios. You can use the combination of &lt;code&gt;promiseTRJANSG&lt;/code&gt;,&lt;br&gt;
&lt;code&gt;promiseTRSANSG&lt;/code&gt; and &lt;code&gt;promiseTRRARNOSG&lt;/code&gt; methods to simulate enough scenarios for&lt;br&gt;
a thorough understanding of promises. Also use of &lt;code&gt;console.time&lt;/code&gt; methods before&lt;br&gt;
and after relevant blocks will help us identify easily if the promises are run&lt;br&gt;
parallelly or sequentially . Let me know if you have any other interesting&lt;br&gt;
scenarios or if I have missed something. If you want all the code samples in a&lt;br&gt;
single place check out this gist.&lt;/p&gt;

&lt;p&gt;Bluebird has some interesting features like&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; Promise.prototype.timeout&lt;/li&gt;
&lt;li&gt; Promise.some&lt;/li&gt;
&lt;li&gt; Promise.promisify&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;We will discuss these in a separate post.&lt;/p&gt;

&lt;p&gt;I will also be writing one more post about my learnings from async and await.&lt;/p&gt;

&lt;p&gt;Before closing I would like to list down all the thumb rules I follow to keep my&lt;br&gt;
head sane around promises.&lt;/p&gt;

&lt;h3&gt;
  
  
  Thumb Rules for using promises
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt; Use promises whenever you are using async or blocking code.&lt;/li&gt;
&lt;li&gt; &lt;code&gt;resolve&lt;/code&gt; maps to &lt;code&gt;then&lt;/code&gt; and &lt;code&gt;reject&lt;/code&gt; maps to &lt;code&gt;catch&lt;/code&gt; for all practical
purposes.&lt;/li&gt;
&lt;li&gt; Make sure to write both &lt;code&gt;.catch&lt;/code&gt; and &lt;code&gt;.then&lt;/code&gt; methods for all the promises.&lt;/li&gt;
&lt;li&gt; If something needs to be done in both the cases use &lt;code&gt;.finally&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt; We only get one shot at mutating each promise.&lt;/li&gt;
&lt;li&gt; We can add multiple handlers to a single promise.&lt;/li&gt;
&lt;li&gt; The return type of all the methods in &lt;code&gt;Promise&lt;/code&gt; object whether they are static
methods or prototype methods is again a &lt;code&gt;Promise&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt; In &lt;code&gt;Promise.all&lt;/code&gt; the order of the promises are maintained in values variable
irrespective of which promise was first resolved.&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>javascript</category>
      <category>promises</category>
      <category>promise</category>
      <category>asyncawait</category>
    </item>
    <item>
      <title>Receiving selected text and URL into our app from other apps.</title>
      <dc:creator>Gokul N K</dc:creator>
      <pubDate>Fri, 16 Nov 2018 08:49:43 +0000</pubDate>
      <link>https://dev.to/nkgokul/receiving-selected-text-and-url-into-our-app-from-other-apps-f56</link>
      <guid>https://dev.to/nkgokul/receiving-selected-text-and-url-into-our-app-from-other-apps-f56</guid>
      <description>&lt;p&gt;We are building a Android app which should receive the selected text and the current URL from browsers like chrome and browser on Android. &lt;/p&gt;

&lt;p&gt;Currently there are separate activities for sharing the selected text and for sharing the URL. But we need both the selected text and the current URL. Is this even possible? If yes how?&lt;/p&gt;

</description>
      <category>reactnative</category>
      <category>javascript</category>
      <category>android</category>
    </item>
  </channel>
</rss>
