MT IT
  • Introduction
  • KEEP IN MIND!!
  • 1️⃣1st month
    • Week 1
      • HTML/CSS
        • HTML
          • HTML Dasar
          • HTML Layouting
          • Learn More
            • Semantic HTML
            • Tables
            • Videos
            • Images
        • CSS
          • CSS Dasar
      • Weekly Review
    • Week 2
      • Bootstrap
        • Tutorial Bootstrap 5
      • Git & Github
      • Responsive
        • Responsive with Bootstrap 5
      • Weekly Review
    • Week 3
      • Javascript
        • Introduction to Javascript
          • What is JavaScript?
          • Brief History of JavaScript
          • How To Add JavaScript to HTML
        • All About Variables
          • Variables
          • Naming JavaScript Variables
          • JavaScript Naming Conventions
          • JavaScript Scope
        • Datatypes
          • What are data types?
          • Primitives and objects
          • Primitive data types
          • Object data types
          • TypeOf Operator
      • Weekly Review
    • Week 4
      • Javascript
        • Data Structures
          • Keyed Collections
          • Indexed collections
        • Equality Comparisons
        • Loops and Iterations
          • The for loop
          • do…while statement
          • while statement
      • Weekly Review
    • Monthly Review
  • 2️⃣2nd Month
    • Week 5
      • Javascript
        • Expressions and Operators
          • Basic operators, maths
          • Assignment operators
          • Comparison operators
          • Logical operators
          • String operators
          • Conditional (ternary) operator
          • Comma operator
        • JavaScript Function
        • Arrow function expressions
        • Built in functions
      • REST - Representational State Transfer
        • API - Application Programming Interface
        • Fetching data from the server
        • The Fetch API
        • Cross-Origin Resource Sharing (CORS)
      • Weekly Review
    • Week 6
      • DOM (Document Object Model)
        • DOM tree
        • Selecting elements
          • getElementById()
          • getElementsByName()
          • querySelector()
        • Manipulating elements
          • createElement()
          • appendChild()
          • textContent
        • Working with Attributes
          • Understanding Relationships Between HTML Attributes & DOM Object’s Properties
          • setAttribute()
          • getAttribute()
          • removeAttribute()
          • hasAttribute()
        • Manipulating Element’s Styles
          • JavaScript Style
          • getComputedStyle()
          • className
          • classList
          • Getting the Width and Height of an Element
        • Working with Events
          • JavaScript Events
          • Handling Events
          • Page Load Events
          • onload
          • Mouse Events
          • Keyboard Events
          • Scroll Events
          • scrollIntoView
      • React JS
        • Getting Started
        • Components Basics
          • Introducing JSX
          • Writing Markup with JSX
          • React Function Components
          • Props vs State
            • State: A Component's Memory
            • How to use Props in React
      • Working with APIs - 1
        • XMLHttpRequest
        • Fetch
      • Weekly Review
    • Week 7
      • Javascript
        • Asynchronous JavaScript
          • Asynchronous JavaScript For Dummies
            • (Pt1): Internals Disclosed!
            • (Pt2): Callbacks
            • (Pt3): Promises
            • (Pt4): Async/Await
        • Callback
        • Promises
          • Promises Basics
          • Promises Chaining
          • Promises Error Handling
        • Async/await
          • async function
        • Tutorial – Learn Callbacks, Promises, & Async/Await in JS by Making Ice Cream
      • React JS
        • Rendering
          • Conditional Rendering
          • Lists and Keys
          • Render Props
        • Hooks
          • useState
          • useEffect
      • Working with APIs - 2
        • Axios
      • React Router Dom
      • Weekly Review
    • Week 8
      • React JS
      • Responsive
      • Chakra UI
      • Firebase
        • Firebase Authentication
      • Weekly Review
    • Monthly Review
  • 3️⃣3rd month
    • Week 9
      • React JS
      • Chakra UI
      • Firebase
      • Axios
      • Weekly Review
    • Week 10
      • React JS
      • Boilerplate
      • Weekly Review
    • Week 11
      • Projects
      • Weekly Review
    • Week 12
      • Projects
      • Weekly Review
    • Project Review
  • 🏁FINAL REVIEW
  • 👇!! Learn More Below !!
  • 🥸Frontend Stack
    • 💻Web Dev
      • React JS
        • Reactstrap
        • React Icons
        • React Router Dom
      • Chakra UI
    • 📱Mobile Dev
      • React Native
        • Introduction
        • Page 1
      • Expo
      • Nativebase
    • 🎽CSS
      • Tailwind
      • Bootstrap
  • ☕Backend Stack
    • Node JS
    • Firebase
      • Authentication
      • Firestore
      • Storage
      • Hosting
      • Cloud Function
      • Emulators
      • RTDB
      • FCM
    • Google Cloud Platform
      • AppEngine
      • Big Query
      • Cloud Functions
      • Cloud Run
      • Cloud Scheduler
      • Cloud SQL
      • Logging
    • Object Relational Mapping (ORM)
      • Sequelize
    • MongoDB
      • MongoDB Realm
    • MySQL
      • Introduction
  • 🦸Fullstack
    • NEXT JS
    • LARAVEL
  • 📦Package
    • Middleware
      • Express JS
    • HTTP client
      • AXIOS
    • 📊Chart
      • Chart.js
      • JSCharting
      • React Google Chart
    • ⏳Date & Time
      • Moment JS
      • Day JS
    • 👨‍💻WYSIWYG Editor
      • Quill JS
      • Slate JS
Powered by GitBook
On this page
  • Promises: Who Are We?
  • Promises: Do You Want Us to Make You a Promise?
  • Promises: Why Us?
  • Promises: What If We Fail?
  • Promises: We Want to Confess Something…
  1. 2nd Month
  2. Week 7
  3. Javascript
  4. Asynchronous JavaScript
  5. Asynchronous JavaScript For Dummies

(Pt3): Promises

Previous(Pt2): CallbacksNext(Pt4): Async/Await

Last updated 1 year ago

of this tutorial, we talked about callbacks and how can we use them to achieve asynchronous programming in JavaScript. In this third part, I'll be introducing you to the world of Promises and how they relate to asynchronous JavaScript. In fact, I promise you that by the end of this story you’ll learn about the what, the how, and the why of promises in JavaScript.

Let’s get into it…

Promises: Who Are We?

Despite the fact that promises ain’t a new concept, they have been officially introduced in the ES6 version of JavaScript. They are mainly used to manage asynchronous tasks in your code.

I highly recommend that you read of this tutorial as it explains how asynchronous code works behind the scenes in JavaScript. Just to have a solid foundation to keep up with this topic.

Okay! Now, time for storytelling…Look at the following scenario…

In this scenario, you’ll be the main character, and you’re playing by the rules of the JavaScript engine. Remember,

  • You can do only one operation at a time.

  • In the order they appear in the scenario.

  • You cannot do any type of I/O operations by yourself.

  • You are productive, you don’t waste any time waiting, and you are always busy doing something.

# The Story…

So, assuming, that one morning, you decided to ask your friend about today’s weather by phone , then drink a cup of coffee— it’s a silly scenario I know, but bear with me for a while…

The first thing you do, you take your phone, then you enter your friend’s phone number, then you press the call button in this order.

As soon as you press the call button, you don’t have any control over how the call is done. There is something happening in the background that you are ignorant about. All you get is a tone telling you that your call is in process…In other words, you’re getting a PROMISE from your phone operator telling you that your call is “pending”, and they are trying to reach out to your friend.

Yet, because you’re playing by the JavaScript engine rules, you are not going to waste your time waiting for them. You just take their promise into consideration and meanwhile move on to do something else you can do. You’re, of course, going to drink your cup of coffee.

Your phone operator is still working on your call…By the end of their work, there would be two possible outcomes,

  • whether the promise is “fulfilled”, and a friend will pick up the phone, and you can ask them about today’s weather

  • or the promise is “rejected”, and something wrong will happen, and more likely your phone operator will tell you why.

End of the storytelling…

# The Takeaway…

The same principles apply when it comes to promises in JavaScript. They are a sort of a garuantee that your asyncrhonous code is being processed.

The Promise object represents the eventual completion (or failure) of an asynchronous operation and its resulting value.

This translates to what I have been trying to illustrate with the storytelling above. But, if we would like to schematise all of this, it would look something like the following,

So, when the promise is initiated in JavaScript (created), its state is “pending”. However, when it finishes executing, its state will be whether “fulfilled” and a value is returned, or “rejected” with a reason (error). A promise is said to be “settled” when it is fulfilled or rejected, but not pending.

Promises: Do You Want Us to Make You a Promise?

Let’s see some code now…

To create a promise in JavaScript, you can use the constructor function with the new keyword. It takes a function as an argument. This function is called the executor function. It always automatically gets invoked (run) when you create a new promise.

If you try the above sample code on your end, you’ll have ‘Execution…’ printed to the console which means that the function is automatically invoked.

resolve and reject are two functions provided by JavaScript for you to use. You can call resolve() in the case of success and reject() in the case of failure.

Let’s see the following,

If you try this, you’ll get the following on your javaScript console…

Notice that in our code we resolved the promise with the value ‘1’. As a result, when we expand the object Promise in the console, we see that it says that the status is “fulfilled” and the result is “1”.

Basically, a promise object in JavaScript holds two main properties,

  • status: pending (initially) | fulfilled (success) | rejected (failure)

  • value: undefined (initially) | any value (fulfilled) | reason (rejected)

A promise represent a proxy for a value that we can get at some point in the future. This is why the promise object defines the then() method that is called whenever the promise is resolved or rejected.

As you can see, the then() method accepts two callbacks, the first one to handle the case of success, and the other to handle the case of failure.

Notice, in the example above, that the value (any value or error) is automatically passed to these callbacks by JavaScript.

The output of running the example code would look like this,

Success: 1

In an ideal world, all asynchronous functions would already return promises.

Promises: Why Us?

Promises are here to solve this problem as they provide us with a mechanism called “chaining”. It makes our code cleaner and easier to deal with.

To demonstrate this, I just want to take the same example we had in the previous story and replicate it here using promises.

We will get the same output as in the example done using callbacks,

However, in a much cleaner and more readable way…This wouldn’t be possible without the chaining feature of promises. The fact that the then() method always returns a promise is what allows us to apply the chain to arrange the order of execution.

Promises: What If We Fail?

Above, we’ve seen that it is possible to catch any errors via the second callback in the executor function. However, you can alternatively handle errors with the catch() method defined in the promise object.

Let’s take the same previous example, and intentionally reject the promise with an error to catch it and stop the flow of execution in the chain,

We intentionally caused an error at line 4. Therefore, the error is caught by the then() which accepts a callback as an argument to print the error.

The output would look something like this,

Promises: We Want to Confess Something…

Promises are amazing for asynchronous programming in JavaScript, but you know what is better? The async/await syntax for a much more readable and cleaner synchronous code. This is going to be the subject of our next story…

Stay tuned…

If you hover over the about promises, you will read,

Before we end this section, I’d like to share with you this quote from the . They state,

One example to provide would be the that you might have already used before to fetch some data from an external source.

In the of this tutorial, we talked about the “callback hell” issue that is cause by using multiple nested callback structures in your code. We said that this could lead to an increase in the difficulty of reading and understanding your code.

2️⃣
the second part
the first part
MDN documentation
MDN documentation
Fetch API
last part