-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Learned about closures with asynchronous code, partial application an…
…d currying and how to preserve states
- Loading branch information
Showing
3 changed files
with
59 additions
and
0 deletions.
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
03 - Chai aur Javascript/16 - Lexical Scoping and Closures/08_closure_with_asynchronous.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Closure with Asynchronous Code | ||
|
||
function fetchData(url){ | ||
|
||
fetch(url) | ||
.then(response => { | ||
if (!response.ok) { | ||
throw new Error(`Network response was not ok`) | ||
} | ||
return response.json() | ||
}) | ||
.then(data => { | ||
console.log(data); | ||
}) | ||
.catch(error => { | ||
console.log( | ||
"There has been a problem with your fetch operation:", | ||
error | ||
); | ||
}) | ||
} | ||
|
||
fetchData("https://api.github.com/users/sohail019"); |
16 changes: 16 additions & 0 deletions
16
... aur Javascript/16 - Lexical Scoping and Closures/09_partial_applications_and_currying.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// Partial applications and currying | ||
|
||
function multiply(x){ | ||
return function(y){ | ||
return x * y | ||
} | ||
} | ||
|
||
|
||
const double = multiply(2) | ||
console.log(double(5)) // 10 | ||
|
||
const triple = multiply(3) | ||
console.log(triple(5)) // 15 | ||
|
||
// Here, multiply is curried function that returns a closure, allowing for partial application of the multiple operation. |
20 changes: 20 additions & 0 deletions
20
03 - Chai aur Javascript/16 - Lexical Scoping and Closures/10_preserving_states.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// Preserving States | ||
|
||
function makeToggle(){ | ||
let isOn = false | ||
|
||
return function(){ | ||
|
||
isOn = !isOn | ||
return isOn | ||
} | ||
} | ||
|
||
const toggle = makeToggle(); | ||
|
||
console.log(toggle()) // true | ||
console.log(toggle()) // false | ||
console.log(toggle()) // true | ||
|
||
// The makeToggle function creates a closure that preserves the isOn state across multiple calls, effectively toggling the state each time it is invoked. | ||
|