By jasonbogd


2010-12-30 00:18:10 8 Comments

I've encountered this term a lot, and even after Googling, still can't understand what exactly it means. Is there some easy-to-understand (ideally with examples) definition of what an asynchronous event is that someone can provide?

Thanks!

11 comments

@epascarello 2010-12-30 05:28:01

Non programming example:

Synchronous You want a pizza for dinner and you are out of the frozen kind. So you have to stop playing WOW which upsets your guild. You go to the kitchen, make the dough, cover it with sauce, add the cheese, and smother it your favorite bacon topping. You just spent 20 minutes of your time making the pizza with another 10 minutes in the oven. The timer beeps and you pull the hot pie out. You can sit back down in front of your computer, eat the pizza, and continue with your raid.

Asynchronous You want a pizza for dinner while playing WOW. You open up a browser window on your 5th monitor. You load up the Pizza website and order your extra cheesy bacon pizza with a side of bacon grease garlic sauce. You go back to your raid and after 20 minutes the door bell rings. You get the pizza. You sit back down in front of your computer, eat the pizza, and continue with your raid.

So what is the difference? One way you waste 20-30 minutes of precious WOW time, the other way you waste $20 plus tip.

@Denis Hoctor 2011-09-04 12:19:08

I feel sad that I find this so funny.

@Iban Dominguez Noda 2014-09-30 11:56:00

I have to admit this might be the best non-programming explanation I´ve seen in a long time! great example!

@JMASTER B 2015-10-08 13:35:28

I get it and this answer help me to understand it, but I want pizza now =/ and the request wouldn't continue until i get pizza -_-

@Cynthia Sanchez 2015-12-28 14:48:04

I love "Non programming examples" !!

@Omar 2016-05-02 16:48:53

@epascarello The funny side to it works really well!!! Not to mention, it is clear to understand. I need to get a hold of you for a project

@Deepak Yadav 2016-12-02 11:32:47

Is there a laugher emoji to add? coz, this is first funniest I found on Stack Overflow

@marcus 2018-04-26 14:54:22

Also synchronous: you order the pizza but you stop playing anyway to wait at the door.

@epascarello 2018-04-26 17:29:02

So where was this linked from that I am now magically getting downvotes after 8 years?

@Teson 2010-12-30 00:20:15

When two different events occur separately from each other, so you can't do

task1
task2

without checking that task1 really finished.

@Grant Gryczan 2018-04-25 20:59:12

If code is synchronous (or sync), it means each piece of code runs in order, sequentially, and the next piece of code cannot run until the previous is completed. Most code is typically synchronous.

If code is asynchronous (or async), it means that code can run separately and independently of other code. If there is async code in the middle of a bunch of sync code, under the context of this particular question, the async code will only run when its event is triggered, regardless of where in the sync code you put it. It is completely separate and independent of the sync code and runs whenever its event says to, not just when the previous piece of code is done running. Some examples of this would be for code that runs on a timed interval, after a file is successfully saved, after a web request is sent, when the user clicks a button, or after an image loads.

@blackHawk 2016-04-16 12:53:09

Asynchronous events are those events that we don't know when it will be occurred in the future for example when server is requested for some file we don't know when it will fulfilled our request or UI events we don't know when user will click on a button or other UI element, but despite of it other things on page or application is happening it does not block anything say the page greyed out all UI until some file from server is not coming or some event is happening all things are independent this is the power of asynchronous events, simply say independent events

@Vitthal Walunj 2015-10-08 04:45:41

Synchronous Vs. Asynchronous Events

Some event handlers are called immediately when the event occurs. These are called ‘synchronous’ events. An example is DocumentNew. It gets called as soon as the user creates a new document.

However, some events are called shortly after the event occurs, usually after a short amount of idle time. These are called ‘asynchronous’ events. They are asynchronous because it would destabalize Source Insight if a user-written macro were to be called at the exact time the event occured.

@chaimp 2010-12-30 02:56:54

An asynchronous event is an event that runs outside the application's main thread.

The best way to understand is to compare to events that run synchronously. The most typical example would be loading a web page.

When you went to this page, you clicked on a link and waited for the page to load and were not able to interact with or use this page until it finished loading. To contrast, if this page were to have an AJAX event (that's Asynchronous JavaScript and XML event) associated with some user action, this page would load some data from another source asynchronously - in parallel (theoretically) with any other actions going on.

Example with Two Synchronous Events (A and B): First A does something. When A is finished B does something.

Example with Two Asynchronous Events (A and B): Both A and B do something at the same time and neither event waits for the other.

@Pointy 2010-12-30 00:30:12

Your page is delivered from the server to a client browser, somewhere out there in the Internet. The browser has drawn the page on a screen, and somebody — or some thing — is looking at it. It's a waiting game. Eyes shift back and forth, taking in this or that detail in quick jumps, darting to the side now and then, away from the screen, to investigate distractions in the environment. The clock ticks. The page glows softly, passively, as the user hovers inactive, hand loosely draped over a mouse, neck bent down and eyes more and more intent on something inviting that your page has to offer.

Suddenly, without any warning at all, the cursor begins to move as the hand on the mouse stiffens slightly and begins nudging the little plastic bump over the rough surface of the table. As the mouse moves, its surrogate on the screen moves in close imitation, grazing past interesting images and witty remarks in the content of your page. Eventually a decision is made, the movement pauses, a muscle or two contract slightly, and the mouse button is depressed by an insistent finger. The microswitch in the mouse triggers an electronic impulse, and suddenly the browser is made aware of what's happened: a mouse click.

In all that, everything about what the user has done while gazing at the page has happened in a way totally unpredictable to the browser, to any client code in your web page, to anything resident on your servers. There was no knowable "wait time" between human actions. The actions, therefore, as transmitted by the equipment hooked to the user's computer, happened when they happened and not according to a predictable clock — that is, they happened asynchronously.

@Felix Kling 2010-12-30 00:35:37

+1 Nice story :) very inviting... it made me moooove my mouse and click the "up vote" arrow. And here is the asynchronous event again....

@sjsam 2015-03-23 18:45:27

Great example, the choice of a browser event is simply great !

@statenjason 2010-12-30 00:45:31

Here's an example of an asynchronous operation in javascript (you need to have your javascript console open)

console.log('One!');
setTimeout(function(){console.log('Two!');},0);
//Doesn't wait
console.log('Three!');};

//OUTPUT:
//One!
//Three!
//Two!

The call to console.log('Two!') will be executed without blocking the rest of the code that happens after it.

In a real scenario, replace setTimeout with someone clicking a button on a webpage. The response to the button click will happen eventually, without blocking other code execution such as page rendering.

@Kai 2010-12-30 00:42:35

Simply put, it means something that occurs after an unknown amount of time, so don't expect immediate results.

For example, "Mom, can I have five dollars?"

Putting my hand out for money is me expecting her to immediately respond by giving me money (synchronous).

Realistically, she will look at me for a moment or two, and then decide to respond when she wants to (asynchronous).

@Rob Weir 2010-12-30 00:30:04

Think of the end of an interview, and they guy says, "Don't call us, we'll call you". That is the essence of an asynchronous event.

Normally you define functions and you call functions explicitly. Your program has a structure where it starts from line 1, then line 2, and except for some conditional code and iterations, calling functions, etc., there is a simple, liner, synchronous structure.

But in some cases you have actions that are triggered by events outside of the direct control of the program, things that come from outside the program, like a user interface events (user clicks the mouse) or a network event (someone tries to connect to your server). Your code does not generate these events directly. They are generated outside of your program, often by the OS based on their monitoring of user interface devices and other systems. These are called asynchronous events.

Just remember, "Don't call us, we'll call you"

@Dejan Marjanovic 2010-12-30 00:21:07

"In programming, asynchronous events are those occurring independently of the main program flow. Asynchronous actions are actions executed in a non-blocking scheme, allowing the main program flow to continue processing."

"With Ajax, web applications can retrieve data from the server asynchronously in the background without interfering with the display and behavior of the existing page."

When you click Edit and Save on SO it is happening asynchronously.

Related Questions

Sponsored Content

33 Answered Questions

[SOLVED] How can I upload files asynchronously?

23 Answered Questions

[SOLVED] How do I completely uninstall Node.js, and reinstall from beginning (Mac OS X)

  • 2012-06-24 13:40:56
  • Dominic Tancredi
  • 1448868 View
  • 1187 Score
  • 23 Answer
  • Tags:   javascript node.js npm

36 Answered Questions

[SOLVED] How do I return the response from an asynchronous call?

5 Answered Questions

[SOLVED] Relation between CommonJS, AMD and RequireJS?

23 Answered Questions

23 Answered Questions

[SOLVED] Event binding on dynamically created elements?

12 Answered Questions

[SOLVED] What exactly are iterator, iterable, and iteration?

14 Answered Questions

8 Answered Questions

[SOLVED] Why use Redux over Facebook Flux?

17 Answered Questions

[SOLVED] How to decide when to use Node.js?

Sponsored Content