টাইপস্ক্রিপ্টে প্রমিস ক্লাস
এই প্রবন্ধটি টাইপস্ক্রিপ্টে প্রমিস ক্লাস ব্যাখ্যা করে।
YouTube Video
প্রমিস
টাইপস্ক্রিপ্টে Promise
ক্লাস আসলে জাভাস্ক্রিপ্টের Promise
এর মতোই। Promise
একটি বিল্ট-ইন ক্লাস যা অ্যাসিঙ্ক্রোনাস অপারেশনগুলি পরিচালনা করে এবং এক্ষেত্রে অপারেশন সম্পন্ন হওয়ার পর এর ফলাফল উপস্থাপন করে। এখানে, আমরা প্রমিস এবং সম্পর্কিত ধারণাগুলির মৌলিক ব্যবহারের ব্যাখ্যা করব।
প্রাথমিক ব্যবহার
প্রমিস তৈরি করা:
একটি Promise
অবজেক্ট অ্যাসিঙ্ক্রোনাস অপারেশনগুলো মোড়ানোর জন্য ব্যবহৃত হয়। Promise
তৈরি হয় new Promise(executor)
এর মাধ্যমে, এবং এটি এর ভিতরে অ্যাসিঙ্ক্রোনাস অপারেশনগুলি সম্পাদন করে। এক্সিকিউটর (executor
) দুটি ফাংশন, resolve
এবং reject
, আর্গুমেন্ট হিসেবে নেয়।
1const myPromise = new Promise<number>((resolve, reject) => {
2 // Perform asynchronous operations
3 const success = true; // This is just an example
4 if (success) {
5 resolve(42); // In case of success
6 } else {
7 reject('Failed'); // In case of failure
8 }
9});
প্রমিস ব্যবহার করা:
তৈরি করা Promise
কে then()
এবং catch()
এর মাধ্যমে প্রক্রিয়াকরণ করা হয়।
1myPromise.then((value) => {
2 console.log(`Success: ${value}`); // Process the value passed with resolve
3}).catch((error) => {
4 console.error(`Error: ${error}`); // Handle the error passed with reject
5});
finally()
মেথড
1const myPromise = new Promise<number>((resolve, reject) => {
2 // Perform asynchronous operations
3 const success = false; // This is just an example
4 if (success) {
5 resolve(42); // In case of success
6 } else {
7 reject('Failed'); // In case of failure
8 }
9});
10
11myPromise.then((value) => {
12 console.log(`Success: ${value}`); // Process the value passed with resolve
13}).catch((error) => {
14 console.error(`Error: ${error}`); // Handle the error passed with reject
15}).finally(() => {
16 console.log('The operation has completed');
17});
finally()
মেথড এমন কোড নির্ধারণ করে যাPromise
পূরণ হোক বা প্রত্যাখ্যাত হোক, সবসময় কার্যকর হবে।
প্রমিস চেইনিং করা:
then()
এর পরে আবার then()
চেইন করার মাধ্যমে, আপনি একাধিক অ্যাসিঙ্ক্রোনাস অপারেশনকে ক্রমান্বয়ে সংযোগ করতে পারেন।
1const myPromise = new Promise<number>((resolve, reject) => {
2 // Perform asynchronous operations
3 const success = true; // This is just an example
4 if (success) {
5 resolve(42); // In case of success
6 } else {
7 reject('Failed'); // In case of failure
8 }
9});
10
11myPromise
12 .then((value) => {
13 console.log(`Success: ${value}`);
14 return value + 1; // Pass the value to the next then
15 })
16 .then((newValue) => {
17 console.log(`New value: ${newValue}`);
18 })
19 .catch((error) => {
20 console.error(`Error: ${error}`);
21 });
async/await:
টাইপস্ক্রিপ্টে, আপনি async
/await
ব্যবহার করে প্রমিসগুলো আরও সহজে পরিচালনা করতে পারেন। একটি async
ফাংশনের মধ্যে, আপনি await
কীওয়ার্ড ব্যবহার করে একটি প্রমিসের ফলাফলের জন্য অপেক্ষা করতে পারেন।
1const myPromise = new Promise<number>((resolve, reject) => {
2 // Perform asynchronous operations
3 const success = true; // This is just an example
4 if (success) {
5 resolve(42); // In case of success
6 } else {
7 reject('Failed'); // In case of failure
8 }
9});
10
11async function asyncFunction() {
12 try {
13 const value = await myPromise;
14 console.log(`Success: ${value}`);
15 } catch (error) {
16 console.error(`Error: ${error}`);
17 }
18}
19
20asyncFunction();
প্রমিস পদ্ধতি
Promise.all()
Promise.all
একাধিক Promise একযোগে চালায় এবং সবগুলো পূরণ না হওয়া পর্যন্ত অপেক্ষা করে।
1const promise1 = Promise.resolve(1);
2const promise2 = Promise.resolve(2);
3const promise3 = Promise.resolve(3);
4
5Promise.all([promise1, promise2, promise3])
6 .then((results) => {
7 console.log(results); // [1, 2, 3]
8 })
9 .catch((error) => {
10 console.error('Error :', error);
11 });
- শুধুমাত্র সব
Promise
পূরণ হলেthen()
কল হয়। যদি একটি Promise-ও ব্যর্থ হয়, তাহলেcatch()
কল হয়।
Promise.race()
Promise.race
একাধিক Promise এর মধ্যে প্রথমে যেটি সম্পন্ন হয়, সেটির ফলাফল ফিরিয়ে দেয়।
1const promise1 = new Promise((resolve) => setTimeout(resolve, 100, 'First'));
2const promise2 = new Promise((resolve) => setTimeout(resolve, 500, 'Second'));
3
4Promise.race([promise1, promise2]).then((result) => {
5 console.log(result); // "First"
6});
- এই উদাহরণের মতো, প্রথমে সম্পন্ন হওয়া
Promise
-এর ফলাফল ফিরিয়ে দেওয়া হয়।
Promise.allSettled()
Promise.allSettled
সব Promise শেষ হওয়া পর্যন্ত অপেক্ষা করে এবং পূরণ ও প্রত্যাখ্যাত উভয় ফলাফল সহ একটি অ্যারে ফিরিয়ে দেয়।
1const promise1 = Promise.resolve(1);
2const promise2 = Promise.reject("Failed");
3const promise3 = Promise.resolve(3);
4
5Promise.allSettled([promise1, promise2, promise3])
6 .then((results) => {
7 results.forEach(result => console.log(result));
8 // [
9 // { status: 'fulfilled', value: 1 },
10 // { status: 'rejected', reason: 'Failed' },
11 // { status: 'fulfilled', value: 3 }
12 // ]
13 });
- সব Promise সম্পন্ন হলেই (পূরণ বা প্রত্যাখ্যাত, যাই হোক)
then()
কল হয়। প্রত্যেকটি ফলাফলে একটিstatus
প্রোপার্টি থাকে ('fulfilled'
বা'rejected'
)। ব্যর্থতা অন্য অপারেশনকে প্রভাবিত করে না।
Promise.any()
Promise.any
প্রথম Promise পূরণ হওয়া পর্যন্ত অপেক্ষা করে এবং সেটি ফিরিয়ে দেয়।
1const promise1 = new Promise((resolve) => setTimeout(() => resolve("A ok"), 300));
2const promise2 = new Promise((_, reject) => setTimeout(() => reject("B fail"), 100));
3const promise3 = new Promise((resolve) => setTimeout(() => resolve("C ok"), 200));
4
5Promise.any([promise1, promise2, promise3])
6 .then(value => {
7 // Logs the first fulfilled value ("C ok")
8 console.log("first fulfilled:", value);
9 })
10 .catch(err => {
11 console.log("should not reach here in this example:", err);
12 });
- এটি একাধিক Promise-এর মধ্যে প্রথম পূরণ হওয়া মানটি ফিরিয়ে দেয়।
সারসংক্ষেপ
টাইপস্ক্রিপ্ট ব্যবহারের মাধ্যমে, আপনি প্রমিসের ধরণ সঠিকভাবে নির্দিষ্ট করতে পারেন, যা আপনাকে আরও টাইপ-সুনির্দিষ্ট প্রোগ্রাম লেখার সুযোগ দেয়। এটি বিকাশের সময় প্রমিস যে ধরণের মান ফেরত দেবে তা আগেই নির্দিষ্ট করার মাধ্যমে ত্রুটি আগে ধরতে সাহায্য করে।
আপনি আমাদের ইউটিউব চ্যানেলে ভিজ্যুয়াল স্টুডিও কোড ব্যবহার করে উপরের নিবন্ধটি অনুসরণ করতে পারেন। দয়া করে ইউটিউব চ্যানেলটিও দেখুন।