Understanding Asynchronous in javascript

Let’s me explain with a simple example:

console.log(‘First log’);
console.log(‘Second log’);
console.log(‘Third log’);

As we can see, every line of code will waits for previous line to complete execution before execute next line. this is calle…


This content originally appeared on DEV Community and was authored by Heru Hartanto

Let's me explain with a simple example:

console.log('First log');
console.log('Second log');
console.log('Third log');

As we can see, every line of code will waits for previous line to complete execution before execute next line. this is called with synchronous.

Here another example:

console.log('First log');
setTimeout(()=>{
    console.log('Second log');
},2000)
console.log('Third log')

setTimeout function will wait for milisecond time that set in second argument and executes anonymous function in the first arguments

First log
Third log
undefined
Second log

As we can see, Third log does not wait for Second log to execute, the method of not waiting for the previous code to complete is called asynchronous.

When we need asynchronous ?

The best way to use asynchronous is when your website working with server to fetching data or getting response, instead of waiting all data from server fully loaded that maybe takes more than one minutes (depend on speed of your internet and server speed to resolve request) you could use asynchronous to make sure that code ahead will execute and javascript will not waiting server response to complete.


This content originally appeared on DEV Community and was authored by Heru Hartanto


Print Share Comment Cite Upload Translate Updates
APA

Heru Hartanto | Sciencx (2021-12-07T22:32:36+00:00) Understanding Asynchronous in javascript. Retrieved from https://www.scien.cx/2021/12/07/understanding-asynchronous-in-javascript/

MLA
" » Understanding Asynchronous in javascript." Heru Hartanto | Sciencx - Tuesday December 7, 2021, https://www.scien.cx/2021/12/07/understanding-asynchronous-in-javascript/
HARVARD
Heru Hartanto | Sciencx Tuesday December 7, 2021 » Understanding Asynchronous in javascript., viewed ,<https://www.scien.cx/2021/12/07/understanding-asynchronous-in-javascript/>
VANCOUVER
Heru Hartanto | Sciencx - » Understanding Asynchronous in javascript. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/12/07/understanding-asynchronous-in-javascript/
CHICAGO
" » Understanding Asynchronous in javascript." Heru Hartanto | Sciencx - Accessed . https://www.scien.cx/2021/12/07/understanding-asynchronous-in-javascript/
IEEE
" » Understanding Asynchronous in javascript." Heru Hartanto | Sciencx [Online]. Available: https://www.scien.cx/2021/12/07/understanding-asynchronous-in-javascript/. [Accessed: ]
rf:citation
» Understanding Asynchronous in javascript | Heru Hartanto | Sciencx | https://www.scien.cx/2021/12/07/understanding-asynchronous-in-javascript/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.