Created
December 2, 2017 16:31
-
-
Save prof3ssorSt3v3/0b05b4e069b074d58bd0623302f4dcc4 to your computer and use it in GitHub Desktop.
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
// loop-recursion.js | |
// Looping vs. Recursion | |
// | |
let fetch = require('node-fetch'); | |
let url = "http://jsonplaceholder.typicode.com/comments?postId=42"; | |
let log = console.log; | |
let count = 0; | |
const SIZE = 120; | |
//Looping | |
log('Starting the loop'); | |
for(let i=0; i<10; i++){ | |
log('inside loop', i); | |
} | |
log('Ended the loop'); | |
//Recursion | |
let recky = function(){ | |
log('inside recky', count); | |
count++; | |
if(count<10){ | |
recky(); | |
} | |
} | |
log('Starting the recursion'); | |
recky(); | |
log('ending the recursion'); | |
//Recursion for countdowns | |
let countdown = function(size){ | |
log('x'.repeat(size)); | |
size = Math.floor( size * 0.95); | |
if(size > 2){ | |
countdown(size); | |
} | |
} | |
countdown(SIZE); | |
//Asynchronous methods | |
// AJAX, Database, Promises, Timers... | |
log('about to fetch'); | |
for(let c=0; c<2; c++){ | |
let d = fetch(url) | |
.then(response=>response.json()) | |
.then(data=>{ log('data', JSON.stringify(data) ) }) | |
log(c, d); | |
} | |
log('after fetch???'); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment