๐ด A webinar by HackOn Hackathon ๐
Join at bit.ly/hackon-js
That's a good question! โ
Do a self analysis of your JS knowledge, take a pause and predict the output for the below code snippet. ๐ฏ
If you're able to predict the correct sequence of log statements, I hope you need not watch the video, skip to the takeaways section. โ๏ธ If not, do watch the video and thank me later! ๐ค
console.log('Hi!');
console.log('Hope you\'re staying safe at home!');
setTimeout(() => console.log('Turn your self isolation into self improvement!'), 1000);
setTimeout(() => console.log('Chant, Go Corona, Corona Go ๐'), 0);
console.log('Thank You!');
GitHub has maximum number of pull requests in JavaScript repositories. Check the live stats here.
-
JavaScript is single threaded, it simply means it has a single call stack.
-
The asynchronous behaviour is not part of the JavaScript language itself, rather they are built on top of the core JavaScript language in the browser (or the programming environment) and accessed through the browser APIs.
-
setTimeout(function, delay)
does not stand for the precise time delay after which the function is executed. It stands for the minimum wait time after which at some point in time the function will be executed.
Take a look at the below code:
function goCorona() {
console.log('Stay Home, Stay Safe');
}
setTimeout(goCorona, 5000);
That doesnโt mean that
goCorona
will be executed in 5s but rather that, in 5000 ms,goCorona
will be added to the queue. The queue, however, might have other events that have been added earlier โ the functiongoCorona
will have to wait. The idea here is the above code gaurantees thatStay Home, Stay Safe
will be printed anytime after5000ms
.
-
setTimeout
returnstimeoutID
- The returnedtimeoutID
is a positive integer value which identifies the timer created by the call tosetTimeout()
. This value can be passed toclearTimeout()
to cancel the timeout. -
The Event Loop has one simple job โ to watch the Call Stack and the Callback Queue. If the Call Stack is empty, it will take the first event from the queue and will push it to the Call Stack, which effectively runs it.
// About me
import { Speaker } from 'HackOn';
let user = {
Name: 'Vinit Shahdeo',
Company: 'Postman',
Profile: 'Software Engineer', // Ex VITian
Twitter: '@Vinit_Shahdeo'
},
title: 'Event loop in JavaScript and rise of Asynchronous behaviour';
_.assign(new Speaker(), user, new Event(title));
Vinit Shahdeo |
Learn more about me here.
Well, in this unprecedented time, you all have showed interest. I thank all of you for joining me. ๐ค Stay Safe at home.
๐ท ๐
/**
*
* Let's fight for Corona together!
*/
function stayAtHome() {
eat();
sleep();
code();
repeat();
}
while(_.isAlive(new Virus('COVID-19'))) {
// Stay home, Stay safe
stayAtHome();
}
๐ You can find the PPT inside
docs
folder.
๐ Click here to download the PPT.
๐๏ธ Feel free to shoot your doubts here.
___ _ ___ ___
/ _ \ | | | \/ |
/ /_\ \___| | __ | . . | ___
| _ / __| |/ / | |\/| |/ _ \
| | | \__ \ < | | | | __/
\_| |_/___/_|\_\ \_| |_/\___|
___ _ _ _
/ _ \ | | | | (_)
/ /_\ \_ __ _ _| |_| |__ _ _ __ __ _
| _ | '_ \| | | | __| '_ \| | '_ \ / _` |
| | | | | | | |_| | |_| | | | | | | | (_| |
\_| |_/_| |_|\__, |\__|_| |_|_|_| |_|\__, |
__/ | __/ |
|___/ |___/
-
Consider leaving a star โญ here.To be open-sourced pretty soon.
// thank you :)
const THANK_YOU_MSG = `Thank you so much for being here!
you were an amazing audience`;
let sayThanks = (viewer) => { return THANK_YOU_MSG };
_.forEach(allViewers, function (viewer) {
_.times(Number.MAX_SAFE_INTEGER, sayThanks(viewer));
});