0

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?

Medo Abdin
  • 185
  • 2
  • 13
  • https://stackoverflow.com/questions/16336367/what-is-the-difference-between-synchronous-and-asynchronous-programming-in-node – Anuresh Verma Jan 31 '21 at 07:55
  • Read about event loop. You'll understand how async works in js. Philip Roberts has a great lecture on it. – itachi Jan 31 '21 at 07:56
  • https://stackoverflow.com/questions/2035645/when-is-javascript-synchronous – Anuresh Verma Jan 31 '21 at 07:56
  • https://stackoverflow.com/questions/51059573/asynchronous-vs-synchronous-code-in-javascript – Anuresh Verma Jan 31 '21 at 07:56
  • https://stackoverflow.com/questions/748175/asynchronous-vs-synchronous-execution-what-does-it-really-mean – Anuresh Verma Jan 31 '21 at 07:57
  • Tons of reference materials: [Sync vs Async](https://www.google.com/search?q=javascript+synchronous+vs+asynchronous&oq=javascript+synchronous+vs+asynchronous) _synchronous code is executed in sequence – each statement waits for the previous statement to finish before executing. Asynchronous code doesn’t have to wait – your program can continue to run. You do this to keep your site or app responsive, reducing waiting time for the user._ – mplungjan Jan 31 '21 at 08:05
  • Does this answer your question? [Asynchronous vs synchronous execution, what does it really mean?](https://stackoverflow.com/questions/748175/asynchronous-vs-synchronous-execution-what-does-it-really-mean) – nsrCodes Jan 31 '21 at 08:10

3 Answers3

1

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...

0

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

Bhumit 070
  • 416
  • 4
  • 12
0

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.

Hayris
  • 1