What does it mean?
asynchronous event is an event that happens suddenly, or almost at the same time when it was expected.
But what is a...
You need to do all the actions one by one, you can't serve two or more customers at the same time. As well as you can't go home until you don't released a buyer.
Is an action that is performed right after another one.
let a;
let b;
function add(a, b) {
return a + b;
}
function print(result) {
console.log('Result of addition A to B is ', result);
}
// ----- Synchronous code example:
a = 5;
b = 3;
const result = add(a, b);
print(result);
coding!
live
coding!
live
coding!
live
it's a function that is triggered by another function, so it let us perform some action when some event occurs
coding!
live
coding!
live
function say(word) {
console.log(word);
}
function whatsup() {
say('what\'s up');
}
function hello() {
say('hello');
whatsup();
}
hello();
> hello
> what's up
hello()
say()
console.log()
hello()
say()
console.log()
hello()
whatsup()
say()
console.log()
whatsup()
say()
console.log()
hello()
function first() {
setTimeout(() => {
console.log('I am first');
}, 0);
}
function second() {
console.log('I am second');
}
function third() {
console.log('I am third');
}
first();
second();
third();
> I am second
first()
setTimeout()
() => console.log()
() => console.log()
() => console.log()
first()
setTimeout()
second()
console.log()
second()
console.log()
third()
console.log()
> I am third
third()
console.log()
() => console.log()
console.log()
> I am first
() => console.log()
console.log()
function first() {
setTimeout(() => console.log('1'), 0);
}
function second() {
console.log('2');
return new Promise((res) => {
res();
console.log('3');
}).then(() => console.log('4'));
}
first();
second();
console.log('5');
> 2
first()
() => console.log()
() => console.log()
() => console.log()
setTimeout()
first()
setTimeout()
console.log()
second()
second()
res()
() => console.log()
second()
console.log()
> 3
second()
console.log()
> 5
() => console.log()
console.log()
> 4
() => console.log()
console.log()
> 1