Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR 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.

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"))
 
PREVIOUS NEXT
Tagged: #synchronous #asynchronous #javascript
ADD COMMENT
Topic
Name
9+4 =