Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

what is asynchronous in 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
Comment

Asynchronous in 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
Comment

Asynchronous in 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
Comment

asynchronous javascript

console.log ('Starting');
let image;

fetch('coffee.jpg').then((response) => {
  console.log('It worked :)')
  return response.blob();
}).then((myBlob) => {
  let objectURL = URL.createObjectURL(myBlob);
  image = document.createElement('img');
  image.src = objectURL;
  document.body.appendChild(image);
}).catch((error) => {
  console.log('There has been a problem with your fetch operation: ' + error.message);
});

console.log ('All done!');
Comment

javascript asynchronous

<script>
    document.write("Hi");
    document.write("<br>");
  
    setTimeout(() => {
        document.write("Let us see what happens");
    }, 2000);
  
    document.write("<br>");
    document.write("End");
    document.write("<br>");
</script>

/*Hi
End
Let us see what happens*/
Comment

Asynchronous JavaScript

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

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

myCalculator(5, 5, myDisplayer);
Comment

PREVIOUS NEXT
Code Example
Javascript :: react component visibility 
Javascript :: javascript booleans 
Javascript :: dynamodb json to regular json 
Javascript :: filtering an array in javascript 
Javascript :: highest and lowest javascript 
Javascript :: react native image picker 
Javascript :: value js 
Javascript :: jsonArray find 
Javascript :: JavaScript try...catch...finally Statement 
Javascript :: new date javascript 
Javascript :: alert in react native 
Javascript :: hasownproperty.call 
Javascript :: private router react v6 
Javascript :: datepicker range npm reactjs 
Javascript :: render html page in javascript 
Javascript :: charat javascript 
Javascript :: send an email react native 
Javascript :: how to remove elements from array 
Javascript :: how to add variables to an array in javascript 
Javascript :: nodejs ecommerce cms 
Javascript :: slice example 
Javascript :: slice js 
Javascript :: Promise.all() with async and await to run in console 
Javascript :: javascript validator 
Javascript :: nodejs input 
Javascript :: componentDidmount event on fonctional component 
Javascript :: Create array literal 
Javascript :: javascript get all options from select 
Javascript :: Child nodes in a node 
Javascript :: react window navigate 
ADD CONTENT
Topic
Content
Source link
Name
3+8 =