From dd5e263b75e971019c8585ea6e2fbe72e9ce254f Mon Sep 17 00:00:00 2001 From: kalon1997 Date: Thu, 12 May 2022 12:21:33 +0530 Subject: [PATCH 1/3] added 48th ques about callbackhell promise async/await --- README.md | 130 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 130 insertions(+) diff --git a/README.md b/README.md index fdae8f6..a83a053 100644 --- a/README.md +++ b/README.md @@ -2107,6 +2107,136 @@ btn.addEventListener('click', clickHandler.handleClick.bind(clickHandler)); +### 48. How to replace callbackhell with Promise or Async/Await with examples ? + +
Answer + +- Part I Callbackhell. +- Calling one callback function inside another and so on is callbackhell. +- First we are defining three functions addTen, subFive and mulTwo. +- These three functions while called with a number, will return a callback. +- The callback function will return either result or error. + +```js +const addTen = (num, callback) => + {return callback(num+10, false)} +``` + +```js +const subFive = (num, callback) => + {return callback(num-5, false)} +``` + +```js +const mulTwo = (num, callback) => + {return callback(num*2, false)} +``` + +- Now lets call these one by one in nested way. +- The result of previous will serve as input for next callback. + +```js +const ans = addTen(5, (addRes, addErr) => { // addRess = 15 + if(!addErr) + { + return subFive(addRes , (subRes, subErr) => { //subRes = 10 + if(!subErr){ + return mulTwo(subRes, (mulRes, mulErr) => { + if(!mulErr) + { + return mulRes; //20 + } + }) + } + }) + } + }) +console.log(ans); // 20 +``` + +- Part II Promise. +- Promise has two parameters resolve and reject. +- Rewrting those three function definations as well, without a callback. + +```js +const addTen = (num) => {return num+10} +``` + +```js +const subFive = (num) => {return num-5} +``` + +```js +const mulTwo = (num) => {return num*2} +``` + +- Creating a promise. + +```js +const promise = new Promise((resolve, reject) => { + if(true) + resolve(5) + else + reject("Something went wrong ") +}) +``` + +- Calling those three functions one by one. +- "then" will keep on returning the result and if any error "catch" will catch it. + +```js +promise.then(addTen).then(subFive).then(mulTwo).then((ans)=>{ +console.log(ans) +}).catch((err)=>{console.log(err)}); +``` + +- Part III Async / Await. +- It actually uses promise internally. + +```js +const addTen = ( num ) => { + return new Promise( ( resolve, reject ) => { + resolve( num+10) + } ) +} +``` + +```js +const subFive = ( num ) => { + return new Promise( ( resolve, reject ) => { + resolve( num-5) + } ) +} +``` + +```js +const mulTwo = ( num ) => { + return new Promise( ( resolve, reject ) => { + resolve( num*2) + } ) +} +``` + +- Put Async keyword before function name and Await before the statments inside the function +- Await will make the later code wait until the result of that statement is returned. +- Always put this inside a try/catch block. + +```js +const ans = async (num) => { + try { + var addRes = await addTen(num); + var subRes = await subFive(addRes); + var mulRes = await mulTwo(subRes); + console.log(mulRes) + } catch (err) { + console.log(err) + } +} +ans(5) +``` + +
+ # Coding Questions ## Passing values by reference vs by value From fd7a01788d0c33f58ec296e622ae653fb4a45dc6 Mon Sep 17 00:00:00 2001 From: Akhil24-abd Date: Mon, 17 Oct 2022 23:39:22 +0530 Subject: [PATCH 2/3] Questions added --- README.md | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/README.md b/README.md index fdae8f6..a4fc97a 100644 --- a/README.md +++ b/README.md @@ -3910,6 +3910,41 @@ personObj.getName2(); +### 8 . What would be the output of the following code ? +```javascript +let a = true; +let c = 0; + +setTimeout(() => { + a = false; +},2000) + +while(a){ + console.log('Hello') +} +``` +
Answer +The above program will print Hello infinitely. Since, Javascript is a single threaded language the actual execution happens only on the main thread. So, setTimeout will wailt for 2000 milliseconds on a seperate thread as while loop has occupied the main thread. The exit condition for the loop is to set the variable a as fasle. But as the loop continously running on the main thread , it a cannot be set false. +
+ +### 9 . What would be the output of the following code ? +```javascript + +let c=0; + +let id = setInterval(() => { + console.log(c++) +},200) + +setTimeout(() => { + clearInterval(id) +},2000) +``` + +
Answer +The above program will print 0 to 9 sequentially. +
+ ## Contributing We always appreciate your feedback on how the book can be improved, and more questions can be added. If you think you have some question then please add that and open a pull request. From 3bb40311689916c371d8a4fba35cfc13c093b774 Mon Sep 17 00:00:00 2001 From: sumeyra davran Date: Sat, 25 Nov 2023 11:37:44 +0300 Subject: [PATCH 3/3] update the answer --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index a4fc97a..42c4b1f 100644 --- a/README.md +++ b/README.md @@ -3120,7 +3120,9 @@ console.log(funcA());
Answer - 1) + 1) funcA Window {...} + innerFunc1 Window {...} + innerFunA11 Window {...}