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 :: class parent and class child 
Javascript :: [jQuery] Moving elements in dom 
Javascript :: Using strings, the spread operator creates an array with each char in the string 
Javascript :: how to create element with class in javascript 
Javascript :: vs code shortkey to launch snippet 
Javascript :: Backbone Model Set Can Set Muliple Values At Once 
Javascript :: allow only numbers in textbox javascript onkeypress 
Javascript :: Error: Minified React error #321 
Javascript :: unreachable code detected javascript 
Javascript :: Mirror Inverse Program in javascript 
Javascript :: 404 error firebase react js 
Javascript :: telerik grid destroy table 
Javascript :: what happens if pass argument to a function that does not have parameters javascript 
Javascript :: payflex api examples php 
Javascript :: onclick add and remove class using jquery 
Javascript :: multi command run in one in terminal npm 
Javascript :: js button that starts programe 
Javascript :: react leaflet layer disable controls while on top 
Javascript :: find the minimum number in an array javascript 
Javascript :: find leap year javascript 
Javascript :: shell curl path of json file as parameter without temporal file 
Javascript :: js organise string tab spaced 
Javascript :: react: render dynamic component from json 
Javascript :: Hexo - Execute Console Commands 
Javascript :: p5.js sketch 
Javascript :: Laravel summernote HTML output 
Javascript :: infinite typing effect react 
Javascript :: Randomly getting error 500 in Azure App Service when downloading angularjs template 
Javascript :: AngularJs - Display Validation Message within Component 
Javascript :: Conditional navigation inside Tabs 
ADD CONTENT
Topic
Content
Source link
Name
3+4 =