The problem is that your asynchronous call LoadCubesJSon()takes time to return, but Koa is unaware of this and continues the flow of control. This is basically it yield.
"Yieldable" objects include promises, generators, and thunks (among others).
'Q' . node-thunkify thunk.
, Q:
var koa = require('koa');
var q = require('q');
var app = koa();
app.use(function *() {
var deferred = q.defer();
setTimeout(function () {
deferred.resolve('Hello World');
}, 1000);
this.body = yield deferred.promise;
});
app.listen(3000);
, :
function * getCubes(next) {
var deferred = q.defer();
_OLAPSchemaProvider.LoadCubesJSon(function (result) {
var output = JSON.stringify(result.toString());
deferred.resolve(output);
});
this.body = yield deferred.promise;
}