Author: M Abo Bakar Aslam

Promises

Promises are a built-in feature in JavaScript used to handle asynchronous operations in a structured and manageable way. They provide a cleaner alternative to callbacks, especially when dealing with complex asynchronous flows.

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

1. Promise Basics

In JavaScript, Promise is a constructor function. When you use it, you create a new promise object.

A Promise connects two parts:

A Promise has three states:

2. Promise Structure

A Promise is created using an executor function, which takes two callback functions:

The result is handled using .then() in consuming code.

// creating promise object "myPromise" that is an instance of Promise constructor
// Promise constructor takes a single argument, which is the executor/anonymous function
// myResolve and myReject are two callback functions
let myPromise = new Promise(function(myResolve, myReject) {
  // "Producing Code" (May take some time)
  
    //only one of following function would be called
    myResolve(value); // calling myResolve when successful and send "value" to the myResolve
    myReject(error);  // calling when error and send "error" to the myReject
})
  
// "Consuming Code" (Must wait for a fulfilled Promise)
myPromise.then(
    function(value) { /* code if successful */ }, //first parameter is for myResolve. Receive "value" from above promise
    function(error) { /* code if some error */ } //second parameter is for myReject. Receive "error" from above promise
)

Example Code 1:

//creating promise object
let prom = new Promise(function(resolve, reject){
        // Asynchronous operation or logic
        let x = 10
        if(x == 10){
        let y = "done"
        resolve(y) //calling resolve() and then control will go to "then" section
        reject(y)//never run because control will not back here after calling above resolve() 
        }else{
            let z="error"
            reject(z)
        }
    }
)
prom.then(
        function(resolveOut){//function will receive value of "y" in "resolveOut"
            console.log("Result of Resolve", resolveOut) //display value of "resolveOut"
        },
        function(rejectOut){//function will receive value of "z" in "rejectOut"
            console.log("Result of Reject", rejectOut) //display value of "rejectOut"
        }
    )

3. Promise with Conditional Logic

Example Code 2:

timeProm = new Promise(function(ourResolve, ourReject){
            let ourMessage = "setTimeout by using Promises" //it should be displayed after 3 seconds
            //now use setTimeout() to send ourMessage to ourResolve
            if(ourMessage != ""){
                                //ourResolve is callback function having an argument
                                //Therefore, we need to define another executor/anonymous function
                                //inside this function, we call our callback function with some parameter
                                //it means
                                //setTimeout(ourResolve(ourMessage), 3000) //logical error
                                setTimeout(function(){ourResolve(ourMessage)}, 3000) //correct instead of above line
            }else{ 
                ourReject("Error for Message")
        }
    })
timeProm.then(
    function(receiveMessage){console.log("Message: ", receiveMessage)},
                    function(receiveError){console.log("Error: ", receiveError)}
)

4. Using Promise with setTimeout()

Example Code 3:

const fileProm = new Promise(
    function(resolve, reject){
        const fsProm = require('fs')
        fsProm.readFile('example1.txt', 'utf8', (error, data)=>{
        //OR you can also use below line. Executor function is used here instead of arrow function
        //fsProm.readFile('example1.txt', 'utf8', function(error, data){
            if(error){
                    errorMessage = "This is error message"
                    reject(errorMessage)
                }else{
                    resolve(data)
                }
        })
    }
)
fileProm.then(
    function(data){console.log("Data Message: ", data)},
    function(errorMessage){console.log("Error Message: ", errorMessage)}
)

5. Promise with File Handling

Example Code 4:

6. Important Characteristics of Promises

7. Advantages of Promises

8. Limitations of Promises

9. Modern Approach

Promises are often used with:

However, modern JavaScript commonly uses:

This approach is built on top of promises and provides a more synchronous-like structure for asynchronous code.

10. Summary