Easy to understand definition of “asynchronous eve

2019-01-02 14:46发布

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条回答
无与为乐者.
2楼-- · 2019-01-02 15:13

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

task1
task2

without checking that task1 really finished.

查看更多
零度萤火
3楼-- · 2019-01-02 15:14

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.

查看更多
呛了眼睛熬了心
4楼-- · 2019-01-02 15:16

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

查看更多
梦该遗忘
5楼-- · 2019-01-02 15:19

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.

查看更多
宁负流年不负卿
6楼-- · 2019-01-02 15:19

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.

查看更多
永恒的永恒
7楼-- · 2019-01-02 15:25

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

查看更多
登录 后发表回答