我显然误解了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必须允许单线程实现,因此阻止它并不酷,但必须有一些模式来粘合异步到同步代码,我刚刚错过了.