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 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 :: code for random password generator in javascript 
Javascript :: javascript get max value in array of objects 
Javascript :: react native raw bottom sheet 
Javascript :: object.map() nest js 
Javascript :: array max in javascript 
Javascript :: javascript get object methods 
Javascript :: text inside image react native 
Javascript :: react js tutorial for beginners 
Javascript :: window location href 
Javascript :: react component did mount function 
Javascript :: js delete url params 
Javascript :: traversing in jquery 
Javascript :: getcontext in javascript 
Javascript :: how to check if input field has value 
Javascript :: usecontext multiple provider 
Javascript :: pimcore 
Javascript :: javascript DOM SELECT 
Javascript :: javascript type 
Javascript :: javascript Adding Element to the Inner Array 
Javascript :: javascript Removing Elements 
Javascript :: JavaScript Generator Throw Method 
Javascript :: missing num 
Javascript :: pyautogui javascript 
Javascript :: How to get prime numbers using for loop in Js 
Javascript :: Cntrlsss:$.Control-Ai 
Javascript :: phaser muy bridge 
Javascript :: Exercice âge JavaScript 
Javascript :: Total amount of points 
Javascript :: HSETNX in redis 
Javascript :: split array by character javascript 
ADD CONTENT
Topic
Content
Source link
Name
5+5 =