0

Assume I have these two functions:

function complex() {
        setTimeout(function() {
                console.log('complex setTimeout');
        }, 1000); // assume this value is random
        function subFun() {
                console.log('complex subFun');
                setTimeout(function() {
                        console.log('complex subFunction setTimeout');
                }, 2000); // assume this value is random
        }
        subFun();
}

function simple() {
        console.log('simple');
}

Assume that I didn't write them - they are third party. Now I want to call them synchronously and get:

complex subFun
complex setTimeout
complex subFunction setTimeout
simple

The problem is, I can't use callbacks (third party functions). I also can't use promises or events (the same reason). I can't use async and step modules, because of functions arity. How the hell can I call simple() after complex() (and all nested sub-functions) synchronously? And do JavaScript sux? :)

4

1 回答 1

1

我怎么能在[异步事物]同步之后调用simple()?

你不能。这就是异步同步的定义。

如果你有没有反馈的异步脚本,你只有两个选择:

  • setInterval通过 a或类似方法轮询脚本的结果,并simple()在检测到已到达后执行
  • 用更好的第三方替换那个第三方
于 2013-03-03T15:38:28.300 回答