forked from javascript-patterns/javascript-patterns
-
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.
- Loading branch information
Showing
4 changed files
with
89 additions
and
24 deletions.
There are no files selected for viewing
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
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,40 @@ | ||
<!doctype html> | ||
<html lang="en"> | ||
<head> | ||
<title>JavaScript Patterns</title> | ||
<meta charset="utf-8"> | ||
</head> | ||
<body> | ||
<script> | ||
/* Title: iife for loops | ||
* Description: loops that make use of counters as expected | ||
*/ | ||
|
||
// this will log 'regular loop 5' | ||
for (var i = 0; i < 5; i++) { | ||
setTimeout(function() { | ||
console.log('regular loop', i); | ||
}, 3000); | ||
} | ||
|
||
// this will log 'iife loop 1', 'iife loop 2', 'iife loop 3', 'iife loop 4' | ||
for (var i = 0; i < 5; i++) { | ||
(function(n) { | ||
setTimeout(function() { | ||
console.log('iife loop', n); | ||
}, 3000); | ||
})(i); | ||
} | ||
|
||
// same results as the iife loop. Let isn't supported by all javascript versions | ||
for (let i = 0; i < 5; i++) { | ||
setTimeout(function() { | ||
console.log('let loop', i); | ||
}, 3000); | ||
} | ||
|
||
// References | ||
// http:https://benalman.com/news/2010/11/immediately-invoked-function-expression/ | ||
</script> | ||
</body> | ||
</html> |
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,45 @@ | ||
<!doctype html> | ||
<html lang="en"> | ||
<head> | ||
<title>JavaScript Patterns</title> | ||
<meta charset="utf-8"> | ||
</head> | ||
<body> | ||
<script> | ||
/* Title: Map and Filter By Reduce | ||
* Description: Combine map and filter with reduce | ||
*/ | ||
|
||
// Basic example | ||
const euro = [29.76, 41.85, 46.5]; | ||
const above30 = euro.reduce((total, amount) => { | ||
if (amount > 30) { | ||
total.push(amount); | ||
} | ||
return total; | ||
}, []); | ||
console.log('above30', above30); | ||
|
||
// Using async/await | ||
async function foo() { | ||
try { | ||
var values = await getValues(); | ||
|
||
return await values.reduce(async function(values, value) { | ||
values = await values; | ||
value = await asyncOperation(value); | ||
console.log(value); | ||
values.push(value); | ||
return values; | ||
}, []); | ||
} catch (err) { | ||
console.log('We had an ', err); | ||
} | ||
} | ||
|
||
// References | ||
// https://medium.freecodecamp.org/reduce-f47a7da511a9 | ||
// https://code.tutsplus.com/tutorials/a-primer-on-es7-async-functions--cms-22367 | ||
</script> | ||
</body> | ||
</html> |
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