[英]How to wait for one function to finish before continuing?
I have three functions. 我有三个功能。 The calling hierarchy is like this: 调用层次结构是这样的:
function a() {
b();
c();
}
function b() {
d();
}
What I want is to finish b then continue doing c. 我想要完成b然后继续执行c。 But d is asynchronous. 但是d是异步的。 I don't have rights to change anything in b and d. 我无权更改b和d中的任何内容。 Is there any way to handle this case? 有什么办法可以处理这种情况? Sorry for my English. 对不起我的英语不好。
Almost certainly not. 几乎可以肯定。 You need a callback, a promise, or an event. 您需要一个回调,一个promise或一个事件。
Since you can't change b()
, you can't add a callback argument to d()
(assuming it accepted one in the first place) and you can't capture the return value of d()
(assuming it returned a promise in the first place). 由于您无法更改b()
,因此无法向d()
添加回调参数(假设它首先接受了一个参数),并且无法捕获d()
的返回值(假设它返回a首先保证)。
We have no way of telling if d()
triggers an event on the DOM when it is done (or even if you are running the JS in a context where there is a DOM). 我们无法确定d()
是否在完成时(或即使您在存在DOM的上下文中运行JS)在DOM上触发事件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.