“Asynchroniczny w JavaScript” Kod odpowiedzi

co jest asynchroniczne w JavaScript

// some functions
function foo() {
	console.log("foo")
}
function bar() {
	console.log("bar")
}

/*  due to the javascript event-loop behavior this code
	is going to be asynchronous but what does that mean?
    
    well, first you need to understand the concept of asynchronous, 
    In computer programming, asynchronous operation means that a 
    process operates independently of other processes. 
*/
setTimeout(foo, 2000)
console.log("faz")
bar()

// this code above is going to print out:
// "faz"
// "bar"
// "foo"

/* this happens because the event loop first executes all the synchronous code 
then waits for the timer to complete and then when it's done puts the callback 
that we passed it in as a first param in something called the task queue where 
it will be added to the call stack and executed
Nirvana-Forever

Asynchroniczny w JavaScript

//Asynchronous JavaScript
console.log('I am first');
setTimeout(() => {
    console.log('I am second')
}, 2000)
setTimeout(() => {
    console.log('I am third')
}, 1000)
console.log('I am fourth')
//Expected output:
// I am first
// I am fourth
// I am third
// I am second
Ariful Islam Adil(Code Lover)

JavaScript to w Settimeout

function func() {
  this.var = 5;
  
  this.changeVar = function() {
    setTimeout(() => { //Don't use function, use arrow function so 'this' refers to 'func' and not window
      this.var = 10;
    }, 1000);
  }
}

var a = new func();
a.changeVar();
TC5550

Asynchroniczny w JavaScript

//Asynchronous JavaScript
console.log('I am first');
setTimeout(() => {
    console.log('I am second')
}, 2000)
setTimeout(() => {
    console.log('I am third')
}, 1000)
console.log('I am fourth')
//Expected output:
// I am first
// I am fourth
// I am third
// I am second
Ariful Islam Adil(Code Lover)

Asynchroniczny JavaScript

function myDisplayer(something) {
  document.getElementById("demo").innerHTML = something;
}

function myCalculator(num1, num2, myCallback) {
  let sum = num1 + num2;
  myCallback(sum);
}

myCalculator(5, 5, myDisplayer);
naly moslih

Odpowiedzi podobne do “Asynchroniczny w JavaScript”

Pytania podobne do “Asynchroniczny w JavaScript”

Więcej pokrewnych odpowiedzi na “Asynchroniczny w JavaScript” w JavaScript

Przeglądaj popularne odpowiedzi na kod według języka

Przeglądaj inne języki kodu