Sometimes, it's hard to comprehend the difference between the two. So Explain it like I am Five.
For further actions, you may consider blocking this person and/or reporting abuse
Sometimes, it's hard to comprehend the difference between the two. So Explain it like I am Five.
For further actions, you may consider blocking this person and/or reporting abuse
Farouk -
Sergi Mamedov -
iAmSherif 💎 -
Anshuman -
Top comments (1)
Sync means that the code will be executed one step at a time and you have to wait until the thing you're currently doing finishes before going to the next step. When your code is async you can do multiple things at once, so you don't need to wait until the current thing finishes to go to the next. Consider this example:
And this one:
If both
aSyncOperation
andanAsyncOperation
return the same result (say, an string or a integer, it doesn't really matter) the first example will print firstly the result and thenOnto the next thing!
while the second one will printOnto the next thing!
and then the result. That's because since the second operation is async your code can continue regardless that operation finished or not.That's why when you're working with Promises you don't get a value back immediately but rather process it inside the
then
callback. Async is the way to go when you want to do something that requires a lot of time, like doing network requests or file reading.JavaScript by itself is always sync, but with Node and some Web APIs you get the illusion that you're doing async things in JavaScript. There's a very good video explaining how this all works: