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 :: javascript reload section 
Javascript :: localstorage.setitem 
Javascript :: import bootstrap 4 in react 
Javascript :: javascript how to convert string to number 
Javascript :: phaser3 align text center 
Javascript :: change dictionary value in React js 
Javascript :: save byte as json string javascript 
Javascript :: react native fontsize not affected by phone settings 
Javascript :: load images js context 
Javascript :: create slug using middleware 
Javascript :: js not not 
Javascript :: ./node_modules/react-draft-wysiwyg/dist/react-draft-wysiwyg.js 
Javascript :: js addeventlistener input searcb mobile 
Javascript :: ajax 
Javascript :: json validator 
Javascript :: suitescript get sublist value 
Javascript :: javascript max date 
Javascript :: Regex for number divisible by 5 
Javascript :: How to make a JSON call to an URL 
Javascript :: to do list app react code 
Javascript :: camel case first javascript 
Javascript :: how to use two text fields in one single row react js 
Javascript :: json.parse 
Javascript :: componentdidmount react hooks 
Javascript :: parse string javascript 
Javascript :: react native section list sort by alphabet 
Javascript :: js check if object key exists 
Javascript :: map within a map javascript 
Javascript :: js how to filter range imutable array 
Javascript :: return new Promise(res = { 
ADD CONTENT
Topic
Content
Source link
Name
6+9 =