Reputation: 5565
When I run this code, this is the result in Chrome Debugging Tools:
need to load metadata after Init finished z
I was expecting the "after init" to appear only once the "finished z" code had fired, however it seems to be returning before. why is this? (I am new to promises and async programming in 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;
}
}
Upvotes: 1
Views: 132
Reputation: 664375
.then(console.log('after Init'));
Here you're calling console.log
and passing the result (undefined
) to then
. You need to pass a callback function, just as you did in initBreezeMetaData
:
dataServiceHelper.initBreezeMetaData().then(function() {
console.log('after Init');
});
return true;
If the initBreezeMetaData
function returns a promise, it should always return a promise. Returning a boolean would cause an exception when the caller wants to invoke a .then
method on the result…
Upvotes: 4