为什么这个呼叫在继续之前没有等待响应
Why does this call not wait for the response before continuing?
当我运行此代码时,这是Chrome调试工具中的结果:
需要加载元数据Init之后成品z
我原以为"after-init"只会在"finished z"代码启动后出现,但它似乎在之前返回。为什么会这样?(我是JS中promise和异步编程的新手)
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
方法时,返回布尔值会导致异常…
相关文章:
- 在单击事件时更新主干模型,并在更改位置之前等待响应
- 如何使用 Ajax 一次仅按顺序发布数组的 1 个值,并在继续之前等待响应
- 如何在 $.post jQuery 中等待响应
- 如何在 ajax 中调用函数到 php 函数并等待响应
- Ajax在返回值之前等待响应
- 为什么这个呼叫在继续之前没有等待响应
- 如何在最里面的循环等待响应时延迟嵌套迭代
- 如何使用async.series来等待响应
- Angularjs$http等待响应
- 当使用带有单个套接字的httpAgent时,它会在发送下一个请求之前等待响应吗?
- Node.js:服务器等待响应和超时
- Google Maps放置API——如何让代码等待响应
- iMacros可以单击按钮而不等待响应吗?
- AngularJS控制器等待响应(或设置回调)
- Chrome扩展运行时.Sendmessage等待响应
- Node.js (node-pg)或C (libpq) -执行PostgreSQL请求而不等待响应
- 在JavaScript中异步加载配置,如何最好等待响应
- 当我们进行ajax调用并等待响应并阻塞页面时显示一个旋转器
- 运行jQuery函数.Ajax等待响应的时间足够长
- js:将记录保存到Ember.数据,等待响应后再显示