Promise In JavaScript In Hindi | JS Promise constructor

Image could not load

JavaScript Promise by Andrew Petrov on Unsplash

Understanding Asynchronous

जैसा कि हम जानते हैं , कि JavaScript Synchronous language है , इसका मतलब है कि अगर किसी statement को execute करने के लिए wait करना पड़ा रहा है तो , तो js engine उस statement को skip करके आगे का code execute कर देगा , और skip किया हुआ code बाद में run होगा ।

For Example

console.log("Start"); // wait for 1 second. setTimeout(function(){ console.log("Running ..."); }, 1000); console.log("End");

output

Start
End
Running ...

output में आप देख सकते हैं कि जिस order में code लिखा गया उसके accordingly result नहीं मिला है इसी behavior को Asynchronous कहते हैं।

और developing के time पर हमारे पास ऐसी कई conditions होती है , जहाँ पर हमें Asynchronous behavior को control करना पड़ता है , जैसे database से data fetch करते समय या कोई API call करते time हम नहीं चाहेंगे कि ये statements skip होकर उससे आगे के statements run हो जाये।

Well , Asynchronous behavior को हम javascript में 3 तरह से handle करते हैं -

और इस blog में हम Promise() Constructor के बारे में बात करेंगे।

JavaScript Promise() constructor

JavaScript में Promise() constructor का use Promise objects create करने के लिए use किया जाता है , जो कि specially asynchronous operations handle करने के लिए use किये जाते हैं। Promises use करने से operations & errors को और अच्छी तरह से handle कर सकते हैं।

Promise() constructor basically 2 Callback functions को as an argument accept करता है , जिन्हे normally resolve, reject नाम देते हैं , हालाँकि आप इन्हे दूसरा नाम भी दे सकते हैं।

code successfully run होने पर हम resolve function call करके data pass करते हैं और error आने पर reject function call करते हैं।

JS Promise Syntax

let promise = new Promise(function(resolve, reject) { if(code_success) { resolve(); // pass data if you want. } else { reject(); // pass error if you want. } });

ये normal promise constructor syntax होता है , जिसके result को कुछ इस तरह से handle किया जाता है -

promise.then( function(result) { /* handle a successful result */ }, function(error) { /* handle an error result */ } );

JS Promise Example

let promise = new Promise(function(resolve, reject) { if(true) { setTimeout(function(){ let fruits = ['Apple', 'Banana', 'Papaya']; resolve(fruits); }, 1000); } else { reject(false); } }); promise.then( function(result) { console.log(result); }, function(error) { console.log(error); } );

Output

Array(3) [ "Apple", "Banana", "Papaya" ].

Promise inside function

normally इसे direct use न करके किसी function के अंदर ही use करते हैं , किसी function के अंदर use करते समय आपको इस return करने की जरूरत नहीं है , और वो function call करते time आप promise object के methods जैसे finally() , catch(), और then() का use कर सकते हैं।

तो suppose कीजिये हमें एक text file text.txt को read करना है , तो कुछ इस तरह से कर सकते हैं।

const fetch_data = (path) => new Promise((resolve, reject) => { let data = { name : "User Name", age : 26 }; if(data) { resolve(data); } else { reject(data); } }); fetch_data() .then(function(result) { console.log(result); }) .catch(function(error) { console.log(error); });

Normally then() में हम proceeded result handle करते हैं , और catch() के through error को handle करते हैं , जैसे

const fetch_data = (path) => new Promise((resolve, reject) => { throw new Error("Custom error in promise"); }); fetch_data() .then(function(result) { console.log(result); }) .catch(function(error) { console.log("Error occured : ", error.message); });

Output

Error occured :  Custom error in promise

तो कुछ इस तरह से JavaScript में Promise() constructor का use किया जाता है , I Hope, आपको समझ आया होगा।

Recent Blogs

Loading ...

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook