“Asynchronous di JavaScript” Kode Jawaban

Apa yang asinkron dalam 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

Asynchronous di 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 ini di 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

Asynchronous di 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 asinkron

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

Jawaban yang mirip dengan “Asynchronous di JavaScript”

Pertanyaan yang mirip dengan “Asynchronous di JavaScript”

Lebih banyak jawaban terkait untuk “Asynchronous di JavaScript” di JavaScript

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya