Promises in JavaScript

Promises in JavaScript

Index

What is a Promise? (Promise क्या है?)

Promises JavaScript में asynchronous operations को handle करने का एक तरीका है। Promises का उपयोग तब किया जाता है जब कोई task asynchronous तरीके से complete होता है और हमें उसके successful completion या failure का इंतजार करना पड़ता है। यह हमें callbacks के बजाय एक structured और readable code लिखने में मदद करता है।

Syntax of a Promise (Promise का Syntax)

let myPromise = new Promise(function(resolve, reject) {
    // Asynchronous code here
    let success = true;

    if (success) {
        resolve("Task completed successfully!");
    } else {
        reject("Task failed!");
    }
});

resolve(): यह function तब call किया जाता है जब task successfully complete होता है।

reject(): यह function तब call किया जाता है जब task fail हो जाता है।

Using a Promise (Promise का उपयोग)

Promises को handle करने के लिए हम .then() और .catch() methods का उपयोग करते हैं।

  • .then(): यह method तब execute होता है जब promise fulfilled (success) होता है।
  • .catch(): यह method तब execute होता है जब promise rejected (fail) होता है।

Example: Basic Promise

let myPromise = new Promise(function(resolve, reject) {
    let taskCompleted = true;

    if (taskCompleted) {
        resolve("The task was successful.");
    } else {
        reject("The task failed.");
    }
});

myPromise.then(function(result) {
    console.log(result); // Output: The task was successful.
}).catch(function(error) {
    console.log(error);  // If rejected: Output: The task failed.
});


JavaScript is Single-Threaded

JavaScript is Single-Threaded

Index

Single-Threaded का मतलब क्या है?

Single-threaded होने का मतलब यह है कि JavaScript का execution एक ही thread में होता है, और यह thread sequentially, यानी एक के बाद एक, commands को process करता है।

Example: Simple Code Execution

function firstTask() {
    console.log("This is the first task.");
}

function secondTask() {
    console.log("This is the second task.");
}

firstTask();
secondTask();

Execution Flow (Flow of the Single Thread):

  • JavaScript सबसे पहले firstTask() को execute करेगा।
  • जब यह complete हो जाएगा, तब secondTask() execute होगा।
  • कोई भी दूसरा code तब तक execute नहीं होगा जब तक कि current task (function) पूरा न हो जाए।

Output:


This is the first task.
This is the second task.

Explore More

Blocking Code (Blocking Behavior in Single-Threaded JS)

How Does JavaScript Handle Asynchronous Tasks?

Single-Threaded vs Multi-Threaded

Conclusion

JavaScript एक single-threaded language है, यानी यह एक समय में एक ही task को execute करता है। इसका मतलब है कि सभी tasks sequentially execute होते हैं। हालांकि, asynchronous tasks को efficiently handle करने के लिए JavaScript के पास Event Loop और Callback Queue जैसी powerful mechanisms हैं, जो इसे non-blocking और responsive बनाते हैं।


JavaScript Call Stack

JavaScript Call Stack

JavaScript में Call Stack एक mechanism है जो functions के execution को manage करता है। Call stack यह track करता है कि कौन-सा function currently execute हो रहा है, और execution के बाद किस function को call किया जाना है। इसे एक stack की तरह imagine किया जा सकता है, जहां functions LIFO (Last In, First Out) order में execute होते हैं।

Call Stack कैसे काम करता है?

Call stack एक stack data structure की तरह काम करता है, जहां functions push होते हैं जब उन्हें call किया जाता है और pop होते हैं जब उनका execution पूरा हो जाता है। जब एक function execute होता है, तो उसे stack में सबसे ऊपर push किया जाता है। जैसे ही उसका execution पूरा होता है, वह stack से pop हो जाता है।

Example: Simple Call Stack Flow

function greet() {
    console.log("Hello!");
}

function sayName() {
    console.log("My name is John.");
    greet();  // Function inside a function
}

function start() {
    console.log("Starting...");
    sayName();  // Calling another function
}

start();

Call Stack Flow:

  • start() function call होता है, तो यह call stack में push होता है।
  • start() के अंदर sayName() function को call किया जाता है, इसलिए यह stack में push हो जाता है।
  • sayName() के अंदर greet() function को call किया जाता है, इसलिए इसे भी stack में push किया जाता है।
  • जब greet() का execution complete होता है, तो यह stack से pop हो जाता है, फिर sayName(), और आखिर में start() भी pop हो जाता है।

Stack Representation:

[ greet() ]   // Call greet() first
[ sayName() ]  // After greet() completes, return to sayName()
[ start() ]    // Finally, return to start()


Call Stack Real-Life Example (Simple Analogy)

Stack Overflow (Call Stack का Overflow)

Most recent Post

Promises in JavaScript

Promises in JavaScript Index What is a Promise? (Promise क्या है?) Syntax of a Promise (Promise का Syntax) Using a Promise...