JavaScript
  • JavaScript Introduction
  • JS Engine
  • V8 Engine
  • First-class function
  • Optimized Code
  • Call Stack & Memory heap
  • Single Thread
  • JavaScript RunTime
  • Nodejs
  • Context and Environment
  • Hoisting
  • Functions
  • Arguments
  • Variables
  • Scope
  • IIFE
  • this
  • call(), apply() and bind()
  • currying
  • Types
  • Type Coercion
  • Functions as Object
  • HOF (Higher Order Function)
  • Two pillars of Javascript
  • Closures
  • Prototypal Inheritance
  • OOP and FP
  • OOP
    • 4 principles of OOP
  • FP
    • Pure function
    • Imperative vs Declarative
    • Immutability
    • HOF and Closures
    • Currying
    • Partial Application
    • Compose and Pipe
  • Composition vs Inheritance
  • OOP vs FP
  • JS working
  • Promises
  • Async Await
  • ES5 - ECMAScript 2009
  • ES6 - ECMAScript 2015
  • ES7 - ECMAScript 2016
  • ES8 - ECMAScript 2017
  • ES9 - ECMAScript 2018
  • ES10 - ECMAScript 2019
  • ES11 - ECMAScript 2020
  • ES12 - ECMAScript 2021
  • JOB Queue
  • Promises Execution
Powered by GitBook
On this page

Was this helpful?

Promises

A Promise is an object that may produce a single value sometime in the future, either a resolves value or a reason that it's not resolved (rejected).

A Promise may in one of three possible states.

  1. pending

  2. fulfilled

  3. rejected

EX: 
const promise = new Promise((resolve, reject) => {
    if (true) {
        resolve("Stuff worked");
    } else {
        reject("Error: Something went wrong");
    }
});

promise.then(res => console.log(res)); // Stuff worked

promise
    .then(res => res + '!')
    .then(res2 => console.log(res2)); // Stuff worked!

promise
    .then(res => res + '!')
    .then(res2 => {
        throw Error('Simple error');
        console.log(res2);
    })
    .catch((err) => console.log(err)); // Simple error
    
promise
    .then(res => res + '!')
    .then(res2 => {
        return res2 + '?';
    })
    .catch((err) => console.log(err))
    .then((res3) => console.log(res3)); // Stuff worked!?

catch is executed only when any error occurred. It will considers the error only it occurred before its placement.

EX:
const promise = new Promise((resolve, reject) => {
    if (true) {
        resolve("Stuff worked");
    } else {
        reject("Error: Something went wrong");
    }
});

const promise2 = new Promise((resolve, reject) => {
    setTimeout(resolve, 100, 'HIII');
});

const promise3 = new Promise((resolve, reject) => {
    setTimeout(resolve, 1000, 'PROOKIE');
});

const promise4 = new Promise((resolve, reject) => {
    setTimeout(resolve, 5000, 'It is me you are loking for me?');
});

Promise.all([promise, promise2, promise3, promise4])
    .then(values => console.log(values));
// ["Stuff worked", "HIII", "PROOKIE", "It is me you are loking for me?"]

Promise all takes multiple promises in array and result will be array of all the results of all promises.

The result will be received only after all the promises execution is completed. If any one of them gets error, all process will be stopped.

PreviousJS workingNextAsync Await

Last updated 5 years ago

Was this helpful?