【发布时间】:2014-06-13 06:04:53
【问题描述】:
假设我有这样的脚本:
function hello() {
var x = 42; // notice the closure over x in the success handler
stuffExecutor.execute({
success: function (result) { println("Success: " + (result + x)); },
failure: function (reason) { println("Failure: " + reason; }
});
println("Starting to execute stuff...");
}
假设stuffExecutor 是一个Java 对象,它有一个execute() 方法,并带有我已放入上下文中的适当签名。
我可以想象实现execute() 方法来推迟它的动作直到hello() 脚本返回之后(从而在成功或失败之前首先打印“开始执行东西......”),但从那里我没有'在延迟执行完成后,不知道如何返回并稍后调用处理程序。特别是,success 处理程序关闭了来自 hello() 函数的局部变量 x,所以我需要以某种方式“取回”旧上下文(或者以其他方式存储它以供以后使用)。
我该怎么做呢?
【问题讨论】:
标签: java javascript asynchronous closures rhino