小编Chr*_*ens的帖子

如何阻止javascript承诺并返回已解决的结果?

我显然误解了js promises的解决方式或者"return"的语义.

我被一个期望我同步的函数调用 - 返回一个值.计算该值需要一些异步代码(特别是dstore Collection上的ForEach方法)

我想要完成的是大约这个,但这不起作用mySynchronousFunction函数没有返回值.

function mySynchronousFunction() {
   var accumulator = {};
   var myPromise = doAsynchronousThingThatSideEffectsAccumulator();
   // Now my caller is expecting the value of accumulator.
   myPromise.then(function() {return accumulator;})
}
Run Code Online (Sandbox Code Playgroud)

我知道JS必须允许单线程实现,因此阻止它并不酷,但必须有一些模式来粘合异步到同步代码,我刚刚错过了.

javascript asynchronous promise deferred

28
推荐指数
2
解决办法
3万
查看次数

标签 统计

asynchronous ×1

deferred ×1

javascript ×1

promise ×1