<?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: Anirban</title>
    <description>The latest articles on DEV Community by Anirban (@anirban_coder).</description>
    <link>https://dev.to/anirban_coder</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%2F423964%2F2f1ef873-60e7-47b5-9c72-3e89b1d7b17d.jpg</url>
      <title>DEV Community: Anirban</title>
      <link>https://dev.to/anirban_coder</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/anirban_coder"/>
    <language>en</language>
    <item>
      <title>Correlation is not the causation!!</title>
      <dc:creator>Anirban</dc:creator>
      <pubDate>Sun, 17 Jan 2021 08:38:28 +0000</pubDate>
      <link>https://dev.to/anirban_coder/correlation-is-not-the-causation-5bj0</link>
      <guid>https://dev.to/anirban_coder/correlation-is-not-the-causation-5bj0</guid>
      <description>&lt;p&gt;Many times we hear that "the correlation is not the causation" or "correlation does not imply causation". But what do they mean? Let's understand this phrase using some statistics and examples because most of the time these terms are mostly misunderstood and often used interchangeably. &lt;/p&gt;

&lt;p&gt;&lt;span&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Feoj85npzu7dzibmhaqa5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Feoj85npzu7dzibmhaqa5.png" alt="Alt Text" width="393" height="382"&gt;&lt;/a&gt;&lt;/span&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  What are even these things?
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Correlation&lt;/strong&gt; is a statistical technique that tells us how strong a pair of variables are linearly related. If we measure this relationship, we will get a number between -1 to 1. The sign of the number denotes how one will change if the other variable changes. If we get a number 0, that means there is no correlation between those two variables. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt; Correlation between ice cream sales and sunglass sold.&lt;/p&gt;

&lt;p&gt;The sales of ice cream increases as the sales of sunglass increases.&lt;/p&gt;

&lt;p&gt;&lt;span&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F2dusg1whs6z20ubwz4o9.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F2dusg1whs6z20ubwz4o9.jpg" alt="Alt Text" width="474" height="270"&gt;&lt;/a&gt;&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Causation&lt;/strong&gt; adds a cause to the relationship. If two variables are correlated then it says any change in one variable will &lt;strong&gt;cause&lt;/strong&gt; change in the other variable. That means these two variables are in cause and effect relationship. One event will help to occur the other event.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt; When the wind blows faster then a windmill produces more power.&lt;br&gt;
Here, the faster wind is the cause to rotate the windmill faster and the effect of this event is more power.&lt;/p&gt;

&lt;p&gt;So, now we understand what correlation and causation are, let's understand why "Correlation does not imply causation!" with the famous example.&lt;/p&gt;

&lt;p&gt;&lt;span&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F5j0p24ntul62lga8u2lk.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F5j0p24ntul62lga8u2lk.jpg" alt="Alt Text" width="420" height="325"&gt;&lt;/a&gt;&lt;/span&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;em&gt;"Ice cream sales are correlated with homicides in New York"&lt;/em&gt;
&lt;/h3&gt;

&lt;p&gt;This hypothesis says that as the sales of ice cream rise and fall, the number of homicides also rise and fall. But does ice cream consumption causing the death of people?&lt;/p&gt;

&lt;p&gt;The answer is "No". If two things are correlated that doesn't mean that one will cause another.&lt;/p&gt;

&lt;p&gt;When two independent things are tied together, these can be either be bound by causality or correlation. But in most cases, it is just a coincidence. Correlation is something we think of when we cannot see under the hood. The less information we have, the more we are forced to see correlations. Similarly, if we have more information then we will be able to see the actual causal relationships.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why correlation is not the causation in this example?
&lt;/h3&gt;

&lt;p&gt;Generally, we collect some samples to test our hypothesis. What if our sample is not large enough and a little too biased? What if there is a hidden factor that we did not record in our sample dataset? These things can influence the causation and correlation of two variables. Here in this example, &lt;strong&gt;weather&lt;/strong&gt; is the hidden factor. &lt;/p&gt;

&lt;p&gt;&lt;span&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Feyetqpi79lc10hbizqff.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Feyetqpi79lc10hbizqff.png" alt="Alt Text" width="595" height="330"&gt;&lt;/a&gt;&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;Let's consider a sunny summer day. It is a lovely day to go outside and people are enjoying the beach. As the day is hot, people are buying ice creams also people are exposed to accidents. The weather is causing a rise in the ice cream sale and homicides. &lt;/p&gt;

&lt;p&gt;&lt;span&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F4l0szignvtoubw3olr2q.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F4l0szignvtoubw3olr2q.jpg" alt="Alt Text" width="676" height="667"&gt;&lt;/a&gt;&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;So, there is no causal relationship between ice cream sales and the homicide rate. Sunny weather is the hidden factor that is bringing both factors together. Also, ice cream sale and the homicide rate both have a causal relationship with the weather. &lt;/p&gt;

&lt;h3&gt;
  
  
  Don't jump to a conclusion too fast!
&lt;/h3&gt;

&lt;p&gt;When we see a correlation is present between two variables, do not jump to a conclusion too early. Always take time to explore if any underlying influencing factor is present or not. If such a factor is present then verify if they are correlated or not and then conclude. You can consider the below questions before concluding. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Do we have enough samples?&lt;/li&gt;
&lt;li&gt;Did we consider the seasonality? &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Hope this post will clear your doubts! &lt;br&gt;
My &lt;a href="https://twitter.com/anirban_coder" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Thanks for reading!!&lt;/p&gt;

</description>
      <category>datascience</category>
      <category>machinelearning</category>
      <category>analytics</category>
      <category>discuss</category>
    </item>
    <item>
      <title>How to setup a cloud deployable machine learning model using Flask 🚀</title>
      <dc:creator>Anirban</dc:creator>
      <pubDate>Fri, 08 Jan 2021 16:35:03 +0000</pubDate>
      <link>https://dev.to/anirban_coder/how-to-setup-a-cloud-deployable-machine-learning-model-using-flask-10a</link>
      <guid>https://dev.to/anirban_coder/how-to-setup-a-cloud-deployable-machine-learning-model-using-flask-10a</guid>
      <description>&lt;p&gt;A well trained machine learnig model is not useful untill we are able to use it to predict on the future data. These kind of predictive models are used to solve several problems. So, we are going to learn how to use a trained machine learning model to predict on future data which will be feed into the model through an API. &lt;/p&gt;

&lt;p&gt;In order to use a maching learning model through a callable API, we need to have a machine learning model in the first place. In this post we are not concentrating to create a model from the scratch (I will write another post on how to create a basic artificial neural network model using tensorflow) instead, we are going to use a model trained using tensorflow and keras libraries on the 'Iris' (flower) dataset. It is very famous dataset and we know that it has 3 classes (&lt;code&gt;setosa&lt;/code&gt;, &lt;code&gt;versicolor&lt;/code&gt;, &lt;code&gt;virginica&lt;/code&gt;) present on the target variable.  &lt;/p&gt;

&lt;p&gt;Another thing we need is the Flask library to create the API. So. let's install flask usnig pip.&lt;br&gt;
&lt;code&gt;pip install flask&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;After installing flask, let's open sublime text3/ any text editor and create a python file. I am going to name this file as &lt;code&gt;my_app.py&lt;/code&gt;. Then I am going to test if the environment is properly setup by creating a basic flask application with only one &lt;code&gt;'/'&lt;/code&gt; route.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from flask import Flask

app = Flask(__name__)

@app.route("/")
def index():
    return '&amp;lt;h1&amp;gt;FLASK APP IS RUNNING!&amp;lt;/h1&amp;gt;'

if __name__ == '__main__':
    app.run()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, let's open the command prompt in the current working directory where the &lt;code&gt;my_app.py&lt;/code&gt; file is located and run the program using &lt;code&gt;python my_app.py&lt;/code&gt; command. We should be able to see in the command prompt like below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Faj27e6exti0lhjygsbpo.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Faj27e6exti0lhjygsbpo.PNG" alt="Alt Text" width="680" height="112"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If we open &lt;code&gt;http://127.0.0.1:5000&lt;/code&gt; in the browser, then we should be able to see the app is running. If you see like below then we are set to proceed.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F3ftt4ibretslt7l9yh7g.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F3ftt4ibretslt7l9yh7g.PNG" alt="Alt Text" width="588" height="181"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, we are going to follow below steps to create an API using Flask and call it through python script.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;First, we will load the model using &lt;code&gt;tensorflow.keras&lt;/code&gt; load_model() method.&lt;/li&gt;
&lt;li&gt;Then, we will load the fitted &lt;code&gt;Scaler&lt;/code&gt; object (i saved as pickle file) using &lt;code&gt;joblib&lt;/code&gt; load() method to transform the new incoming data.&lt;/li&gt;
&lt;li&gt;Create a function to be able to take a model object, a scaler object and the new data to predict.&lt;/li&gt;
&lt;li&gt;Create an API endpoint using &lt;code&gt;Flask&lt;/code&gt; .&lt;/li&gt;
&lt;li&gt;Test the API using &lt;code&gt;postman&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Call the API using Python script and get the prediction.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Load the model
&lt;/h3&gt;

&lt;p&gt;We are going to import load_model from &lt;code&gt;tensorflow.keras.models&lt;/code&gt; as the model is build using tensorflow. The model can be saved by any method.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;from tensorflow.keras.models import load_model&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Then we will use below code to load the model in my variable called &lt;code&gt;flower_model&lt;/code&gt;. Here, I saved my model as &lt;code&gt;my_model.h5&lt;/code&gt; in the current working directory.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;flower_model = load_model("my_model.h5")&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Load the scaler object
&lt;/h3&gt;

&lt;p&gt;I saved my fitted MinMaxScaler object in a pickle file in this current working directory as &lt;code&gt;my_scaler.pkl&lt;/code&gt;. So, I am going to use &lt;code&gt;joblib&lt;/code&gt; to load this scaler object in my variable called &lt;code&gt;flower_scaler&lt;/code&gt;. Before that, we need to import the joblib module.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import joblib
flower_scaler = joblib.load("my_scaler.pkl")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Creating a function that return prediction
&lt;/h3&gt;

&lt;p&gt;We are going to create a function that can take a model, a scaler and a data. The data is going to be in JSON format. So, we are going to read each field and will save into variables. This way we can create a list with similar shape of the training features. We have 4 features in this case. These are &lt;code&gt;sepal_length&lt;/code&gt;, &lt;code&gt;sepal_width&lt;/code&gt;,&lt;code&gt;petal_length&lt;/code&gt;,&lt;code&gt;petal_width&lt;/code&gt;. Then we will transform the data using scaler object and will use the model to predict the classes. The function code is given below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def return_prediction(model, scaler, data):
    s_len = data["sepal_length"]    
    s_wid = data["sepal_width"]    
    p_len = data["petal_length"]    
    p_wid = data["petal_width"]

    classes = np.array(['setosa', 'versicolor', 'virginica'])

    flower = [[s_len, s_wid, p_len, p_wid]]

    flower = scaler.transform(flower)

    class_ind = model.predict_classes(flower)[0]

    return classes[class_ind]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;em&gt;We need to &lt;code&gt;import numpy as np&lt;/code&gt; in the top in order to convert the list labels (['setosa', 'versicolor', 'virginica']) into numpy array&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Creating API endpoint using Flask
&lt;/h3&gt;

&lt;p&gt;I am going to create a route like &lt;code&gt;/api/flower&lt;/code&gt; which can respond to a HTTP Post method. I am going to use request module to extract the JSON data posted by a client. Then I am going to use &lt;code&gt;return_prediction()&lt;/code&gt; function to predict on this new data and return a JSON object using &lt;code&gt;jsonify&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@app.route("/api/flower", methods=['POST'])
def flower_prediction():
    content = request.json
    result = return_prediction(flower_model, flower_scaler, content)
    return jsonify(result)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;em&gt;We need to import &lt;code&gt;request&lt;/code&gt; and &lt;code&gt;jsonify&lt;/code&gt; modules from flask library&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;from flask import request, jsonify&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Testing the API using Postman
&lt;/h3&gt;

&lt;p&gt;Now, I am going to run the program using below command in command prompt from the working directory. &lt;code&gt;python my_app.py&lt;/code&gt;. This will launch the server and we are ready to test the API.&lt;/p&gt;

&lt;p&gt;Now, let's open Postman and issue a post request to the endpoint &lt;code&gt;http://127.0.0.1:5000/api/flower&lt;/code&gt;. We will create a body of the request in JSON format and will send the request.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
    "sepal_length":5.1,
    "sepal_width":3.5,
    "petal_length":1.4,
    "petal_width":0.2
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fn7opjus7lpf8u1n9nx23.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fn7opjus7lpf8u1n9nx23.PNG" alt="Alt Text" width="800" height="212"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can see the status code is 200 OK and the API has returned it's prediction as 'setosa' based on the input data.&lt;/p&gt;

&lt;h3&gt;
  
  
  Calling the API using Python script
&lt;/h3&gt;

&lt;p&gt;Next, we are going to write a python script to call the API. We need to import request library in order to do that.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;import requets&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Then we will create a dictionary object to pass through the API.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;flower_example = {
    "sepal_length":5.1,
    "sepal_width":3.5,
    "petal_length":1.4,
    "petal_width":0.2
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finally, we will call the API using post method and print the result.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;result = requests.post("http://127.0.0.1:5000/api/flower", json=flower_example)
print(result)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fxmb8gi1zt08uj2xm7zsg.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fxmb8gi1zt08uj2xm7zsg.PNG" alt="Alt Text" width="800" height="161"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That's all for today. We will talk about how to deploy this model into cloud in my next post.&lt;br&gt;
&lt;a href="https://twitter.com/anirban_coder" rel="noopener noreferrer"&gt;My Twitter for any suggestion&lt;/a&gt;&lt;br&gt;
Have a great one!&lt;/p&gt;

</description>
      <category>python</category>
      <category>machinelearning</category>
      <category>flask</category>
    </item>
    <item>
      <title>JavaScript Promise</title>
      <dc:creator>Anirban</dc:creator>
      <pubDate>Thu, 09 Jul 2020 21:18:26 +0000</pubDate>
      <link>https://dev.to/anirban_coder/i-promise-1d24</link>
      <guid>https://dev.to/anirban_coder/i-promise-1d24</guid>
      <description>&lt;p&gt;A promise in JavaScript is an object that may produce a single value any time in the future. It may give us either a resolved value or a reason that it is not resolved (rejected). A promise can be in one of the following three states. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Fulfilled&lt;/li&gt;
&lt;li&gt;Rejected&lt;/li&gt;
&lt;li&gt;Pending&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We can create a promise like below. The constructor function of the promise takes a function that has parameters that either resolves or rejects.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;promise&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&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="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;It worked&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Error, it broke&lt;/span&gt;&lt;span class="dl"&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, we can check the returned value from the promise by chaining with &lt;code&gt;.then&lt;/code&gt; like below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This should log "It worked" in the console as the promise was resolved.&lt;/p&gt;

&lt;p&gt;We can catch the rejected value comes out of the promise by chaining with &lt;code&gt;.catch&lt;/code&gt; like below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;promise&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&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="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;It worked&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Error, it broke&lt;/span&gt;&lt;span class="dl"&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;promise&lt;/span&gt;
&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This code should write "Error, it broke" in the console.&lt;/p&gt;

&lt;p&gt;Also, we can get the resolved results from the multiple promises using &lt;code&gt;Promise.all()&lt;/code&gt; method.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;promise1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&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="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;setTimeout&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="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Promise1&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;promise2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&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="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;setTimeout&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="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Promise2&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;promise3&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&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="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;setTimeout&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="mi"&gt;5000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Promise3&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;all&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="nx"&gt;promise1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;promise2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;promise3&lt;/span&gt;&lt;span class="p"&gt;]).&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;results&lt;/span&gt;&lt;span class="p"&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="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;results&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;p&gt;Here, we will get an array as an output in the console after 5 seconds. The output will look like &lt;code&gt;[ 'Promise1', 'Promise2', 'Promise3' ]&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;That's all for today. If you like this post and want to talk more about it, please feel free to &lt;a href="https://twitter.com/anirban_coder" rel="noopener noreferrer"&gt;DM me here&lt;/a&gt;&lt;br&gt;
Have a great one!&lt;/p&gt;

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