Created
April 15, 2020 00:14
-
-
Save sagarjhaa/c1549ed47119944d67881158a5c23bc1 to your computer and use it in GitHub Desktop.
How to work with Async
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
// http://csbin.io/async | |
/* | |
-Async functions calls methods in browser. | |
-They reutrn a promise. | |
-Once promise is resolved or rejected the methods related to .then are called. | |
-This methods are put in the micro-queue which the event loop check after the global space is cleared and before checking | |
the callback queue. | |
|---> Global Space (stack | |
| | |
| | |
Event |---> micro - queue | |
Loop | | |
| | |
|---> callback queue. | |
*/ | |
/* CHALLENGE 1 */ | |
function sayHowdy() { | |
console.log('Howdy'); | |
} | |
function testMe() { | |
setTimeout(sayHowdy, 0); | |
console.log('Partnah'); | |
} | |
// After thinking it through, uncomment the following line to check your guess! | |
testMe(); // what order should these log out? Howdy or Partnah first? | |
/* CHALLENGE 2 */ | |
function delayedGreet() { | |
setTimeout(() => console.log('welcome'),3000); | |
} | |
// Uncomment the following line to check your work! | |
delayedGreet(); // should log (after 3 seconds): welcome | |
/* CHALLENGE 3 */ | |
function helloGoodbye() { | |
// ADD CODE HERE | |
console.log('hello'); | |
setTimeout(() => console.log('good bye'),3000); | |
} | |
// Uncomment the following line to check your work! | |
// helloGoodbye(); // should log: hello // should also log (after 3 seconds): good bye | |
/* CHALLENGE 4 */ | |
function brokenRecord() { | |
// ADD CODE HERE | |
} | |
// Uncomment the following line to check your work! | |
// brokenRecord(); // should log (every second): hi again | |
/* CHALLENGE 5 */ | |
function limitedRepeat() { | |
// ADD CODE HERE | |
var test = () => { | |
console.log('hi for now'); | |
} | |
var index = 0; | |
var interval = setInterval(() => { | |
index+=1; | |
if (index > 5){ | |
clearInterval(interval); | |
} | |
else{ | |
test() | |
} | |
},1000) | |
} | |
// Uncomment the following line to check your work! | |
// limitedRepeat(); // should log (every second, for 5 seconds): hi for now | |
/* CHALLENGE 6 */ | |
function everyXsecsForYsecs(func,x,y) { | |
// ADD CODE HERE | |
var totalTime = 1; | |
var interval = setInterval(()=>{ | |
if(x*totalTime <= y){ | |
totalTime++; | |
func(); | |
} | |
else{ | |
clearInterval(interval); | |
} | |
},x) | |
} | |
// Uncomment the following lines to check your work! | |
function theEnd() { | |
console.log('This is the end!'); | |
} | |
function sayHi(){ | |
console.log('hi.'); | |
} | |
// everyXsecsForYsecs(theEnd, 2, 20); // should invoke theEnd function every 2 seconds, for 20 seconds): This is the end! | |
// everyXsecsForYsecs(sayHi, 1000,5000); | |
/* CHALLENGE 7 */ | |
function delayCounter(target, wait) { | |
let index = 1 | |
return function(){ | |
var interval = setInterval(() => { | |
if (index > target){ | |
clearInterval(interval); | |
} | |
console.log(index); | |
index+=1 | |
},wait) | |
} | |
} | |
// UNCOMMENT THESE TO TEST YOUR WORK! | |
// const countLogger = delayCounter(10, 1000) | |
// countLogger(); | |
// After 1 second, log 1 | |
// After 2 seconds, log 2 | |
// After 3 seconds, log 3 | |
/* CHALLENGE 8 */ | |
function promised (val) { | |
// ADD CODE HERE | |
return new Promise(function(resolve, reject) { | |
// do a thing, possibly async, then… | |
setTimeout(() => { | |
resolve(val); | |
},2000); | |
}) | |
} | |
// UNCOMMENT THESE TO TEST YOUR WORK! | |
// const createPromise = promised('wait for it...'); | |
// createPromise.then((val) => console.log(val)); | |
// will log "wait for it..." to the console after 2 seconds | |
/* CHALLENGE 9 */ | |
class SecondClock { | |
constructor(cb) { | |
// ADD CODE HERE | |
this.cb = cb; | |
this.initial = 0; | |
} | |
start(){ | |
this.interval = setInterval(()=>{ | |
this.initial+=1; | |
this.cb(this.initial%11); | |
},1000); | |
} | |
reset(){ | |
this.initial = 0; | |
clearInterval(this.interval); | |
} | |
// ADD METHODS HERE | |
} | |
// UNCOMMENT THESE TO TEST YOUR WORK! | |
// const clock = new SecondClock((val) => { console.log(val) }); | |
// console.log("Started Clock."); | |
// clock.start(); | |
// setTimeout(() => { | |
// clock.reset(); | |
// console.log("Stopped Clock after 6 seconds."); | |
// }, 61000); | |
/* CHALLENGE 10 */ | |
function debounce(callback, interval) { | |
// ADD CODE HERE | |
let isFree = true; | |
let intervalTimer; | |
return function(){ | |
if(isFree){ | |
isFree = false; | |
return callback(); | |
} | |
intervalTimer = setTimeout(() => isFree=true,interval) | |
} | |
} | |
// UNCOMMENT THESE TO TEST YOUR WORK! | |
function giveHi() { return 'hi'; } | |
const giveHiSometimes = debounce(giveHi, 3000); | |
console.log(giveHiSometimes()); // -> 'hi' | |
setTimeout(function() { console.log(giveHiSometimes()); }, 2000); // -> undefined | |
setTimeout(function() { console.log(giveHiSometimes()); }, 4000); // -> undefined | |
setTimeout(function() { console.log(giveHiSometimes()); }, 8000); // -> 'hi' | |
setTimeout(function() { console.log(giveHiSometimes()); }, 9000); // -> 'undefined' | |
setTimeout(function() { console.log(giveHiSometimes()); }, 12090); // -> 'hi' | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment