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 个解决方案
#1
425
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.
同步你想要一个比萨饼吃晚餐,你就是冷冻的。所以你必须停止玩WOW,这会扰乱你的公会。你去厨房,做面团,盖上酱汁,加入奶酪,然后扼杀你最喜欢的培根馅料。你花了20分钟的时间在烤箱里再做10分钟的披萨。定时器发出哔哔声,然后拉出热馅饼。你可以坐在电脑前,吃披萨,继续你的突袭。
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.
异步你在玩WOW时想吃披萨。您在第5台显示器上打开浏览器窗口。你加载比萨网站,并订购你的额外俗气培根披萨与培根油蒜酱。你回到你的突袭中,20分钟后,门铃响了。你得到了比萨饼。你坐在电脑前,吃披萨,继续你的突袭。
So what is the difference? One way you waste 20-30 minutes of precious WOW time, the other way you waste $20 plus tip.
那么区别是什么呢?一种方法是浪费20-30分钟的宝贵WOW时间,另一种方式是浪费20美元加上小费。
#2
21
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.
您的页面从服务器传送到客户端浏览器,位于Internet的某个位置。浏览器在屏幕上绘制了页面,有人 - 或者某些东西 - 正在查看它。这是一场等待的比赛。眼睛来回移动,在快速跳跃中接受这个或那个细节,偶尔向侧面飞去,远离屏幕,以调查环境中的干扰。时钟滴答作响。当用户徘徊不活动时,页面会被轻柔地,被动地发光,手放在鼠标上方松散,脖子向下弯曲,眼睛越来越倾向于邀请您的页面提供的东西。
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.
总而言之,用户在注视页面时所做的一切都以一种完全无法预测的方式发生在浏览器,网页中的任何客户端代码以及服务器上的任何内容中。人类行为之间没有可知的“等待时间”。因此,由连接到用户计算机的设备传输的动作发生在它们发生时而不是根据可预测的时钟 - 也就是说,它们是异步发生的。
#3
18
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.
通常,您定义函数并显式调用函数。你的程序有一个从第1行开始,然后是第2行的结构,除了一些条件代码和迭代,调用函数等,还有一个简单的线性同步结构。
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"
请记住,“别打电话给我们,我们会打电话给你”
#4
12
"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."
“使用Ajax,Web应用程序可以在后台异步检索服务器中的数据,而不会干扰现有页面的显示和行为。”
When you click Edit and Save on SO it is happening asynchronously.
单击“编辑并在SO上保存”时,它将异步发生。
#5
12
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.
当您转到此页面时,您单击了一个链接并等待加载该页面,并且在完成加载之前无法与该页面进行交互或使用此页面。相比之下,如果此页面具有与某些用户操作相关联的AJAX事件(即异步JavaScript和XML事件),则此页面将异步加载来自另一个源的一些数据 - 并行(理论上)与正在进行的任何其他操作。
Example with Two Synchronous Events (A and B): First A does something. When A is finished B does something.
两个同步事件(A和B)的示例:First A做了一些事情。当A完成时B做了一些事情。
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.
两个异步事件(A和B)的示例:A和B都同时执行某些操作,并且两个事件都不等待另一个事件。
#6
7
Here's an example of an asynchronous operation in javascript (you need to have your javascript console open)
这是javascript中的异步操作示例(您需要打开javascript控制台)
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.
对console.log('Two!')的调用将在不阻塞其后发生的其余代码的情况下执行。
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.
在实际场景中,将某个人点击网页上的按钮替换为setTimeout。最终会发生对按钮单击的响应,而不会阻止其他代码执行,例如页面呈现。
#7
5
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?"
例如,“妈妈,我可以有5美元吗?”
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).
实际上,她会看着我一两个小时,然后决定在她想要的时候做出反应(异步)。
#8
2
When two different events occur separately from each other, so you can't do
当两个不同的事件彼此分开发生时,你就做不到了
task1
task2
without checking that task1
really finished.
没有检查task1真的完成了。
#9
1
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
异步事件是我们不知道将来何时发生的事件,例如,当某个文件请求服务器时我们不知道它何时会满足我们的请求或UI事件我们不知道何时用户将点击一个按钮或其他UI元素,但尽管如此,页面上的其他内容或应用程序正在发生它不会阻止任何说明页面灰显所有UI直到服务器的某些文件未来或某些事件发生所有事情都是独立的这是异步事件的力量,简单地说是独立事件
#10
0
Synchronous Vs. Asynchronous Events
同步VS.异步事件
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.
事件发生时会立即调用某些事件处理程序。这些被称为“同步”事件。一个例子是DocumentNew。一旦用户创建新文档,就会调用它。
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.
但是,事件发生后不久会调用一些事件,通常是在短暂的空闲时间之后。这些被称为“异步”事件。它们是异步的,因为如果在事件发生的确切时间调用用户编写的宏,它将使Source Insight不稳定。
#11
0
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.
如果代码是异步的(或异步的),则意味着代码可以独立于其他代码运行。如果在一堆同步代码的中间存在异步代码,则在此特定问题的上下文中,异步代码将仅在其事件被触发时运行,而不管您在其中放置同步代码的位置。它是完全独立的,独立于同步代码,并在其事件发生时运行,而不仅仅是在前一段代码完成运行时。这样的一些示例是针对在定时间隔上运行的代码,在成功保存文件之后,在发送web请求之后,当用户单击按钮时,或在图像加载之后运行的代码。
#1
425
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.
同步你想要一个比萨饼吃晚餐,你就是冷冻的。所以你必须停止玩WOW,这会扰乱你的公会。你去厨房,做面团,盖上酱汁,加入奶酪,然后扼杀你最喜欢的培根馅料。你花了20分钟的时间在烤箱里再做10分钟的披萨。定时器发出哔哔声,然后拉出热馅饼。你可以坐在电脑前,吃披萨,继续你的突袭。
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.
异步你在玩WOW时想吃披萨。您在第5台显示器上打开浏览器窗口。你加载比萨网站,并订购你的额外俗气培根披萨与培根油蒜酱。你回到你的突袭中,20分钟后,门铃响了。你得到了比萨饼。你坐在电脑前,吃披萨,继续你的突袭。
So what is the difference? One way you waste 20-30 minutes of precious WOW time, the other way you waste $20 plus tip.
那么区别是什么呢?一种方法是浪费20-30分钟的宝贵WOW时间,另一种方式是浪费20美元加上小费。
#2
21
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.
您的页面从服务器传送到客户端浏览器,位于Internet的某个位置。浏览器在屏幕上绘制了页面,有人 - 或者某些东西 - 正在查看它。这是一场等待的比赛。眼睛来回移动,在快速跳跃中接受这个或那个细节,偶尔向侧面飞去,远离屏幕,以调查环境中的干扰。时钟滴答作响。当用户徘徊不活动时,页面会被轻柔地,被动地发光,手放在鼠标上方松散,脖子向下弯曲,眼睛越来越倾向于邀请您的页面提供的东西。
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.
总而言之,用户在注视页面时所做的一切都以一种完全无法预测的方式发生在浏览器,网页中的任何客户端代码以及服务器上的任何内容中。人类行为之间没有可知的“等待时间”。因此,由连接到用户计算机的设备传输的动作发生在它们发生时而不是根据可预测的时钟 - 也就是说,它们是异步发生的。
#3
18
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.
通常,您定义函数并显式调用函数。你的程序有一个从第1行开始,然后是第2行的结构,除了一些条件代码和迭代,调用函数等,还有一个简单的线性同步结构。
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"
请记住,“别打电话给我们,我们会打电话给你”
#4
12
"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."
“使用Ajax,Web应用程序可以在后台异步检索服务器中的数据,而不会干扰现有页面的显示和行为。”
When you click Edit and Save on SO it is happening asynchronously.
单击“编辑并在SO上保存”时,它将异步发生。
#5
12
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.
当您转到此页面时,您单击了一个链接并等待加载该页面,并且在完成加载之前无法与该页面进行交互或使用此页面。相比之下,如果此页面具有与某些用户操作相关联的AJAX事件(即异步JavaScript和XML事件),则此页面将异步加载来自另一个源的一些数据 - 并行(理论上)与正在进行的任何其他操作。
Example with Two Synchronous Events (A and B): First A does something. When A is finished B does something.
两个同步事件(A和B)的示例:First A做了一些事情。当A完成时B做了一些事情。
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.
两个异步事件(A和B)的示例:A和B都同时执行某些操作,并且两个事件都不等待另一个事件。
#6
7
Here's an example of an asynchronous operation in javascript (you need to have your javascript console open)
这是javascript中的异步操作示例(您需要打开javascript控制台)
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.
对console.log('Two!')的调用将在不阻塞其后发生的其余代码的情况下执行。
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.
在实际场景中,将某个人点击网页上的按钮替换为setTimeout。最终会发生对按钮单击的响应,而不会阻止其他代码执行,例如页面呈现。
#7
5
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?"
例如,“妈妈,我可以有5美元吗?”
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).
实际上,她会看着我一两个小时,然后决定在她想要的时候做出反应(异步)。
#8
2
When two different events occur separately from each other, so you can't do
当两个不同的事件彼此分开发生时,你就做不到了
task1
task2
without checking that task1
really finished.
没有检查task1真的完成了。
#9
1
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
异步事件是我们不知道将来何时发生的事件,例如,当某个文件请求服务器时我们不知道它何时会满足我们的请求或UI事件我们不知道何时用户将点击一个按钮或其他UI元素,但尽管如此,页面上的其他内容或应用程序正在发生它不会阻止任何说明页面灰显所有UI直到服务器的某些文件未来或某些事件发生所有事情都是独立的这是异步事件的力量,简单地说是独立事件
#10
0
Synchronous Vs. Asynchronous Events
同步VS.异步事件
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.
事件发生时会立即调用某些事件处理程序。这些被称为“同步”事件。一个例子是DocumentNew。一旦用户创建新文档,就会调用它。
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.
但是,事件发生后不久会调用一些事件,通常是在短暂的空闲时间之后。这些被称为“异步”事件。它们是异步的,因为如果在事件发生的确切时间调用用户编写的宏,它将使Source Insight不稳定。
#11
0
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.
如果代码是异步的(或异步的),则意味着代码可以独立于其他代码运行。如果在一堆同步代码的中间存在异步代码,则在此特定问题的上下文中,异步代码将仅在其事件被触发时运行,而不管您在其中放置同步代码的位置。它是完全独立的,独立于同步代码,并在其事件发生时运行,而不仅仅是在前一段代码完成运行时。这样的一些示例是针对在定时间隔上运行的代码,在成功保存文件之后,在发送web请求之后,当用户单击按钮时,或在图像加载之后运行的代码。