Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

synchronous vs asynchronous in javascript / ES6

// synchronous - Means execute the next one even though the previous 
// has not finished yet

// asynchronous - Means wait for the previous operation to finish and then execute
// the next one 

// sync example 
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"))

// asynchronous example 
// the callback must wait for the username to be displayed 1st
function getUsername(callback) {
    setTimeout(() => {
        console.log("Alextz")
        callback()
    }, 2000)
}

getUsername(() => console.log("getUsername callback called"))
Source by codecraft.tv #
 
PREVIOUS NEXT
Tagged: #synchronous #asynchronous #javascript
ADD COMMENT
Topic
Name
6+1 =