Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

what is synchronous and asynchronous in javascript

Synchronous:
- In synchronous operations tasks are performed one at a time and only when one 
is completed, the following is unblocked. In other words, you need to wait for a
task to finish to move to the next one.

Asynchronous:
- In asynchronous operations, you can move to another task 
before the previous one finishes.
Comment

what is synchronous and asynchronous in javascript

Synchronous: In synchronous operations tasks are performed one at a time and only when one 
is completed, the following is unblocked. In other words, you need to wait for a
task to finish to move to the next one.

function getUsername(callback) {
    setTimeout(() => {
        console.log("Alextz")
        callback()
    }, 2000)
}

getUsername(() => console.log("getUsername callback called"))

Asynchronous:  In asynchronous operations, you can move to another task 
before the previous one finishes.


function getUser(callback) {
    setTimeout(() => {
        console.log("Alextz")
    }, 2000)

    // get executed even though the user has not printed yet
    callback()
}

getUser(() => console.log("getUser callback called"))
Comment

js use await in synchronous method

var btn = document.getElementById("btn");

btn.addEventListener("click", handler, false);

function handler(e) {
  console.log("handler triggered");
  doSomething();
  console.log("handler done");
}

function doSomething() {
  doThis();
  doThat();
  doTheOther();
}

function doThis() {
  console.log("doThis - start & end");
}
function doThat() {
  console.log("doThat - start");
  // do something that takes a while
  var stop = Date.now() + 1000;
  while (Date.now() < stop) {
    // wait
  }
  console.log("doThat - end");
}
function doTheOther() {
  console.log("doThat - start & end");
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: reverse integer in for javascript 
Javascript :: prisma.db mysql 
Javascript :: js object delete value by key 
Javascript :: nodejs cdn 
Javascript :: angular store select method 
Javascript :: array max 
Javascript :: object constructor js 
Javascript :: use index of an array within a for loop 
Javascript :: es6 class example 
Javascript :: dom 
Javascript :: base64 
Javascript :: node.js Readable Streams 
Javascript :: javascript object as key 
Javascript :: how to change port in next js 
Javascript :: aws lambda function setup for node js 
Javascript :: how to upgrade nodejs version 
Javascript :: how to add a new line in template literal javascript 
Javascript :: hamburger menu js 
Javascript :: sails js 
Javascript :: html form action javascript method 
Javascript :: JavaScript Debug usage Example 
Javascript :: context api in react 
Javascript :: change size of font awesome icon react 
Javascript :: run react native with debugger breakpoint 
Javascript :: proptypes for a react component 
Javascript :: string en javascript 
Javascript :: google chart ajax json 
Javascript :: how to display ä in js 
Javascript :: You are getting a `numbers` array. Return the sum of **negative** numbers only. //condition to check for negative 
Javascript :: angular generer guard 
ADD CONTENT
Topic
Content
Source link
Name
3+9 =