Medo Abdin
Medo Abdin

Reputation: 315

what is the difference between synchronous and asynchronous in Javascript?

I'm confusing between those two terms: (synchronously and asynchronously) in Javascript I searched a lot I found many sources talking about that but no source explains that obviously and easily.

the synchronous in English it's means something runs at the same time or simultaneously but unlike the asynchronously that doesn't run at the same time. so, from these two terms synchronous JS execute tasks that you want synchronously let's suppose two tasks at the same time I need it to work so I think the name of it is synchronous tasks, not asynchronous tasks, Is that right?

but when I read some sources I found that terms apply to asynchronous in JS and not synchronous terms.

so, I want to know the difference between synchronous and asynchronous in Javascript simply to understand and, where I should use both of them?

Upvotes: 0

Views: 3276

Answers (3)

Hayris
Hayris

Reputation: 1

synchronous code is executed sequentially. Each line waits for the previous line to finish before executing. If the code is asynchronous, your program can continue to run. It is especially useful for extracting data from the database. You do this to reduce the waiting time of the user.

Upvotes: 0

Ahmad Salihu
Ahmad Salihu

Reputation: 13

Synchronous function execute your code one after the other it can only process one code at a time till the code stack is empty while Asynchronous means you can skip a a steps within a code. For example lets say you are building an app and you request a data from online and that data is what your user see when the visit your website, then if there is network delay in the site you request the data your page will not open till it fetches that data. While if you used asynchronous function, you might decide to render some part of your app so that your users will not have to staring at a blank page(that is not a good user experience). O hope i answered your question but if it's not still clear let me know i can help demonstrate by using some code as an example...

Upvotes: 1

Bhumit 070
Bhumit 070

Reputation: 426

synchronous code is something which runs in queue , suppose if you have 5 lines of code then the next line code will wait for the previous line to complete the execution and then it will execute

example of synchronous code

async function getTodos() {
  const resp = await  fetch("https://jsonplaceholder.typicode.com/todos/1")
    .then(async (response) => await response.json())
    .then((json) => json);
  console.log(resp);
}

in above code the resp will wait for the api to give the results and then wait it will print but if we do not make it synchronous the code will keep running and it will not wait for data to come and print the result in console

function getTodos() {
  const resp = fetch("https://jsonplaceholder.typicode.com/todos/1")
    .then(async (response) => await response.json())
    .then((json) => json);
  console.log(resp);
}

run it on your own machine to get more clarifications

Upvotes: 0

Related Questions