[英]Why does this call not wait for the response before continuing?
当我运行此代码时,这是Chrome调试工具中的结果:
需要在Init完成z后加载元数据
我希望“after init”只在“完成的z”代码被触发后出现,但它似乎在之前返回。 为什么是这样? (我是JS中的承诺和异步编程的新手)
Class1.js:
function activate()
{
dataServiceHelper.initBreezeMetaData().then(console.log('after Init'));
}
dataServiceHelper.js:
function initBreezeMetaData()
{
if (manager.metadataStore.isEmpty())
{
console.log('need to load metadata');
return manager.fetchMetadata().then(function ()
{
console.log('loaded metadata for ' + config.serviceUrl);
}).fin(function ()
{
console.log('finished z');
});
} else
{
return true;
}
}
.then(console.log('after Init'));
在这里,您将调用 console.log
并将结果( undefined
)传递给then
。 您需要传递一个回调函数,就像在initBreezeMetaData
:
dataServiceHelper.initBreezeMetaData().then(function() {
console.log('after Init');
});
return true;
如果initBreezeMetaData
函数返回一个promise,它应该总是返回一个promise。 当调用者想要对结果调用.then
方法时,返回布尔值会导致异常...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.