“Asynchrone en javascript” Réponses codées

Qu'est-ce que asynchrone en 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

Asynchrone en 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 ceci dans 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

Asynchrone en 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 asynchrone

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

Réponses similaires à “Asynchrone en javascript”

Questions similaires à “Asynchrone en javascript”

Plus de réponses similaires à “Asynchrone en javascript” dans JavaScript

Parcourir les réponses de code populaires par langue

Parcourir d'autres langages de code