From 834eded01774775b151414da59c93be73283c74c Mon Sep 17 00:00:00 2001 From: Rahul Arora <108695777+Rahul12Arora@users.noreply.github.com> Date: Wed, 22 Feb 2023 17:00:39 +0530 Subject: [PATCH 1/8] promise definition added --- README.md | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/README.md b/README.md index cae796b..820cafe 100644 --- a/README.md +++ b/README.md @@ -652,3 +652,35 @@ Promise: An object that is used as a placeholder for the future result of an asy TDZ: Temporal Dead Zone is the period of time during which the let and const declarations cannot be accessed. Temporal Dead Zone starts when the code execution enters the block which contains the let or const declaration and continues until the declaration has executed. + + + + +Promise Working +//Making a new promise(it recieves a function with 2 arguments) : syntax is given below +const p = new Promise(function(resolve,reject){ + let a = 1+1; // what promise does or fetches i.e definition functionality defined here + + if(a==2){ + resolve("success") //pass anything in resolve eg. a message in this case + } + else( + reject("failed") //pass anything in reject eg. a message in this case + ) +}) + +// Using promise => p has a callback function in then & catch, then recieves resolve response as argument & catch recieves reject in it's callback argument +p.then((message)=>{ + console.log("we are in then which return the resolve response as : ", message) +}).catch((message)=>{ + console.log("we are in catch which returns the reject response as :", message) +}) + + +Running multiple promises at once => Promise.all (messages contains responses of all promises) +Promise.all([ promise1, promise2, promise3 ]).then((messages)=>{ // .all takes array of promises as an argument, +console.log(messages)}) // .then has a callback function that takes messages array as an argumrnt which has promise responses + +Running multiple promises at once => Promise.race (message is first promise that runs) +Promise.all([ promise1, promise2, promise3 ]).then((messages)=>{ // .all takes array of promises as an argument, +console.log(messages)}) // .then has a callback function that takes message as an argumrnt of the first promise to respond From fc23553d9156f5c5c008e6769448d393c2abfd2c Mon Sep 17 00:00:00 2001 From: Rahul Arora <108695777+Rahul12Arora@users.noreply.github.com> Date: Thu, 23 Feb 2023 19:19:57 +0530 Subject: [PATCH 2/8] Update README.md --- README.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 820cafe..fe2faaa 100644 --- a/README.md +++ b/README.md @@ -656,8 +656,8 @@ TDZ: Temporal Dead Zone is the period of time during which the let and const dec -Promise Working -//Making a new promise(it recieves a function with 2 arguments) : syntax is given below +*Promise Working +// 1.) Making a new promise (it recieves a function with 2 arguments) : syntax is given below const p = new Promise(function(resolve,reject){ let a = 1+1; // what promise does or fetches i.e definition functionality defined here @@ -669,12 +669,13 @@ const p = new Promise(function(resolve,reject){ ) }) -// Using promise => p has a callback function in then & catch, then recieves resolve response as argument & catch recieves reject in it's callback argument +// 2.) Using promise => p has a callback function in then & catch, then recieves resolve response as argument & catch recieves reject in it's callback argument p.then((message)=>{ console.log("we are in then which return the resolve response as : ", message) }).catch((message)=>{ console.log("we are in catch which returns the reject response as :", message) }) +// *** .then() method returns a promise whose resolve value is what we return in current .then(), so now we can chain promises with .then() Running multiple promises at once => Promise.all (messages contains responses of all promises) From 8a2f01d312228443f0ce51b85dafb7859cba7a0e Mon Sep 17 00:00:00 2001 From: Rahul Arora <108695777+Rahul12Arora@users.noreply.github.com> Date: Thu, 23 Feb 2023 19:49:12 +0530 Subject: [PATCH 3/8] Update README.md --- README.md | 48 +++++++++++++++++++++++++++++++++++------------- 1 file changed, 35 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index fe2faaa..6405c29 100644 --- a/README.md +++ b/README.md @@ -656,9 +656,9 @@ TDZ: Temporal Dead Zone is the period of time during which the let and const dec -*Promise Working -// 1.) Making a new promise (it recieves a function with 2 arguments) : syntax is given below -const p = new Promise(function(resolve,reject){ +## Promise Working +**1.) Making a new promise (it recieves a function with 2 arguments) : syntax is given below.** +`const p = new Promise(function(resolve,reject){ let a = 1+1; // what promise does or fetches i.e definition functionality defined here if(a==2){ @@ -667,21 +667,43 @@ const p = new Promise(function(resolve,reject){ else( reject("failed") //pass anything in reject eg. a message in this case ) -}) +})` -// 2.) Using promise => p has a callback function in then & catch, then recieves resolve response as argument & catch recieves reject in it's callback argument -p.then((message)=>{ +**2.) Using promise => p has a callback function in then & catch, then recieves resolve response as argument & catch recieves reject in it's callback argument.** +`p.then((message)=>{ console.log("we are in then which return the resolve response as : ", message) }).catch((message)=>{ console.log("we are in catch which returns the reject response as :", message) }) -// *** .then() method returns a promise whose resolve value is what we return in current .then(), so now we can chain promises with .then() +// *** .then() method returns a promise whose resolve value is what we return in current .then(), so now we can chain promises with .then()` +***Example of promise chaining*** +` +const firstPromiseReturner = function(){ + return new Promise(function(resolve,reject){ + resolve("My cat is named "); + reject("error in P1"); + }) +} +const secondPromiseReturner = function(datafromfirstpromise){ + return new Promise(function(resolve,reject){ + resolve(datafromfirstpromise + "TOM"); + reject("error in P2"); + }) +} + + +firstPromiseReturner().then((resolveValue)=>{ + return secondPromiseReturner(resolveValue); +}).then((resolveValue)=>{ + console.log(resolveValue); +}) +` -Running multiple promises at once => Promise.all (messages contains responses of all promises) -Promise.all([ promise1, promise2, promise3 ]).then((messages)=>{ // .all takes array of promises as an argument, -console.log(messages)}) // .then has a callback function that takes messages array as an argumrnt which has promise responses +**Running multiple promises at once => Promise.all (messages contains responses of all promises)** +`Promise.all([ promise1, promise2, promise3 ]).then((messages)=>{ // .all takes array of promises as an argument, +console.log(messages)}) // .then has a callback function that takes messages array as an argumrnt which has promise responses` -Running multiple promises at once => Promise.race (message is first promise that runs) -Promise.all([ promise1, promise2, promise3 ]).then((messages)=>{ // .all takes array of promises as an argument, -console.log(messages)}) // .then has a callback function that takes message as an argumrnt of the first promise to respond +**Running multiple promises at once => Promise.race (message is first promise that runs)** +`Promise.all([ promise1, promise2, promise3 ]).then((messages)=>{ // .all takes array of promises as an argument, +console.log(messages)}) // .then has a callback function that takes message as an argumrnt of the first promise to respond` From 9ae3fbaa33d4501f6837168b3aa99a200fe4dc09 Mon Sep 17 00:00:00 2001 From: Rahul Arora <108695777+Rahul12Arora@users.noreply.github.com> Date: Thu, 23 Feb 2023 19:50:54 +0530 Subject: [PATCH 4/8] Update README.md --- README.md | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 6405c29..f915977 100644 --- a/README.md +++ b/README.md @@ -659,13 +659,13 @@ TDZ: Temporal Dead Zone is the period of time during which the let and const dec ## Promise Working **1.) Making a new promise (it recieves a function with 2 arguments) : syntax is given below.** `const p = new Promise(function(resolve,reject){ - let a = 1+1; // what promise does or fetches i.e definition functionality defined here + let a = 1+1; what promise does or fetches i.e definition functionality defined here if(a==2){ - resolve("success") //pass anything in resolve eg. a message in this case + resolve("success") pass anything in resolve eg. a message in this case } else( - reject("failed") //pass anything in reject eg. a message in this case + reject("failed") pass anything in reject eg. a message in this case ) })` @@ -675,7 +675,7 @@ TDZ: Temporal Dead Zone is the period of time during which the let and const dec }).catch((message)=>{ console.log("we are in catch which returns the reject response as :", message) }) -// *** .then() method returns a promise whose resolve value is what we return in current .then(), so now we can chain promises with .then()` +.then() method returns a promise whose resolve value is what we return in current .then(), so now we can chain promises with .then()` ***Example of promise chaining*** ` const firstPromiseReturner = function(){ @@ -701,9 +701,9 @@ firstPromiseReturner().then((resolveValue)=>{ **Running multiple promises at once => Promise.all (messages contains responses of all promises)** -`Promise.all([ promise1, promise2, promise3 ]).then((messages)=>{ // .all takes array of promises as an argument, -console.log(messages)}) // .then has a callback function that takes messages array as an argumrnt which has promise responses` +`Promise.all([ promise1, promise2, promise3 ]).then((messages)=>{ .all takes array of promises as an argument, +console.log(messages)}) .then has a callback function that takes messages array as an argumrnt which has promise responses` **Running multiple promises at once => Promise.race (message is first promise that runs)** -`Promise.all([ promise1, promise2, promise3 ]).then((messages)=>{ // .all takes array of promises as an argument, -console.log(messages)}) // .then has a callback function that takes message as an argumrnt of the first promise to respond` +`Promise.all([ promise1, promise2, promise3 ]).then((messages)=>{ .all takes array of promises as an argument, +console.log(messages)}) .then has a callback function that takes message as an argumrnt of the first promise to respond` From c1f645de4c15fe329ac30317e2fc83956bdd7936 Mon Sep 17 00:00:00 2001 From: Rahul Arora <108695777+Rahul12Arora@users.noreply.github.com> Date: Thu, 23 Feb 2023 19:53:19 +0530 Subject: [PATCH 5/8] Update README.md --- README.md | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index f915977..9d9cc67 100644 --- a/README.md +++ b/README.md @@ -670,15 +670,16 @@ TDZ: Temporal Dead Zone is the period of time during which the let and const dec })` **2.) Using promise => p has a callback function in then & catch, then recieves resolve response as argument & catch recieves reject in it's callback argument.** -`p.then((message)=>{ +```p.then((message)=>{ console.log("we are in then which return the resolve response as : ", message) }).catch((message)=>{ console.log("we are in catch which returns the reject response as :", message) }) -.then() method returns a promise whose resolve value is what we return in current .then(), so now we can chain promises with .then()` +``` +**.then() method returns a promise whose resolve value is what we return in current .then(), so now we can chain promises with .then()** ***Example of promise chaining*** -` -const firstPromiseReturner = function(){ + +```const firstPromiseReturner = function(){ return new Promise(function(resolve,reject){ resolve("My cat is named "); reject("error in P1"); @@ -697,13 +698,13 @@ firstPromiseReturner().then((resolveValue)=>{ }).then((resolveValue)=>{ console.log(resolveValue); }) -` +``` **Running multiple promises at once => Promise.all (messages contains responses of all promises)** -`Promise.all([ promise1, promise2, promise3 ]).then((messages)=>{ .all takes array of promises as an argument, -console.log(messages)}) .then has a callback function that takes messages array as an argumrnt which has promise responses` +```Promise.all([ promise1, promise2, promise3 ]).then((messages)=>{ .all takes array of promises as an argument, +console.log(messages)}) .then has a callback function that takes messages array as an argumrnt which has promise responses``` **Running multiple promises at once => Promise.race (message is first promise that runs)** -`Promise.all([ promise1, promise2, promise3 ]).then((messages)=>{ .all takes array of promises as an argument, -console.log(messages)}) .then has a callback function that takes message as an argumrnt of the first promise to respond` +```Promise.all([ promise1, promise2, promise3 ]).then((messages)=>{ .all takes array of promises as an argument, +console.log(messages)}) .then has a callback function that takes message as an argumrnt of the first promise to respond``` From 3b44ff2f16af03bb4491cd9260e0b5c778afcf4c Mon Sep 17 00:00:00 2001 From: Rahul Arora <108695777+Rahul12Arora@users.noreply.github.com> Date: Mon, 27 Feb 2023 01:13:33 +0530 Subject: [PATCH 6/8] Update README.md --- README.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/README.md b/README.md index 9d9cc67..94c2c1c 100644 --- a/README.md +++ b/README.md @@ -708,3 +708,18 @@ console.log(messages)}) .then has a **Running multiple promises at once => Promise.race (message is first promise that runs)** ```Promise.all([ promise1, promise2, promise3 ]).then((messages)=>{ .all takes array of promises as an argument, console.log(messages)}) .then has a callback function that takes message as an argumrnt of the first promise to respond``` + +``` +async function doWork(){ +try{ + const response = await makeRequest('Facebook') + console.log('Response Received') + + const processedResponse = await processRequest(response) + console.log('processedResponse) + } +catch(err){ + console.log(err)} +} +doWork() +``` From f73dac0fc0654d79086eeb0f70e3f5b0c493b411 Mon Sep 17 00:00:00 2001 From: Rahul Arora <108695777+Rahul12Arora@users.noreply.github.com> Date: Mon, 27 Feb 2023 01:14:48 +0530 Subject: [PATCH 7/8] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 94c2c1c..6bda5db 100644 --- a/README.md +++ b/README.md @@ -714,7 +714,6 @@ async function doWork(){ try{ const response = await makeRequest('Facebook') console.log('Response Received') - const processedResponse = await processRequest(response) console.log('processedResponse) } @@ -722,4 +721,5 @@ catch(err){ console.log(err)} } doWork() + ``` From 3cf54a716797fa29216f1b260ba1b6a1c332558c Mon Sep 17 00:00:00 2001 From: Rahul Arora <108695777+Rahul12Arora@users.noreply.github.com> Date: Mon, 27 Feb 2023 01:17:39 +0530 Subject: [PATCH 8/8] Update README.md --- README.md | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 6bda5db..e148d4a 100644 --- a/README.md +++ b/README.md @@ -658,19 +658,21 @@ TDZ: Temporal Dead Zone is the period of time during which the let and const dec ## Promise Working **1.) Making a new promise (it recieves a function with 2 arguments) : syntax is given below.** -`const p = new Promise(function(resolve,reject){ +``` +const p = new Promise(function(resolve,reject){ let a = 1+1; what promise does or fetches i.e definition functionality defined here - if(a==2){ resolve("success") pass anything in resolve eg. a message in this case } else( reject("failed") pass anything in reject eg. a message in this case ) -})` +}) +``` **2.) Using promise => p has a callback function in then & catch, then recieves resolve response as argument & catch recieves reject in it's callback argument.** -```p.then((message)=>{ +``` +p.then((message)=>{ console.log("we are in then which return the resolve response as : ", message) }).catch((message)=>{ console.log("we are in catch which returns the reject response as :", message) @@ -679,7 +681,8 @@ TDZ: Temporal Dead Zone is the period of time during which the let and const dec **.then() method returns a promise whose resolve value is what we return in current .then(), so now we can chain promises with .then()** ***Example of promise chaining*** -```const firstPromiseReturner = function(){ +``` +const firstPromiseReturner = function(){ return new Promise(function(resolve,reject){ resolve("My cat is named "); reject("error in P1"); @@ -702,12 +705,16 @@ firstPromiseReturner().then((resolveValue)=>{ **Running multiple promises at once => Promise.all (messages contains responses of all promises)** -```Promise.all([ promise1, promise2, promise3 ]).then((messages)=>{ .all takes array of promises as an argument, -console.log(messages)}) .then has a callback function that takes messages array as an argumrnt which has promise responses``` +``` +Promise.all([ promise1, promise2, promise3 ]).then((messages)=>{ .all takes array of promises as an argument, +console.log(messages)}) .then has a callback function that takes messages array as an argumrnt which has promise responses +``` **Running multiple promises at once => Promise.race (message is first promise that runs)** -```Promise.all([ promise1, promise2, promise3 ]).then((messages)=>{ .all takes array of promises as an argument, -console.log(messages)}) .then has a callback function that takes message as an argumrnt of the first promise to respond``` +``` +Promise.all([ promise1, promise2, promise3 ]).then((messages)=>{ .all takes array of promises as an argument, +console.log(messages)}) .then has a callback function that takes message as an argumrnt of the first promise to respond +``` ``` async function doWork(){