Javascript/sails.js Controller Waiting For Service To Call Async Function
It is very straightforward to use an asnyc call to wait for a Javascript function to complete, however it appears to be less clear how one should handle a Controller calling a Serv
Solution 1:
In chatting with the wonderful community on #sailsjs on IRC I have found a far better answer than dealing with passing callbacks around (hate that design pattern).
varPromise = require('bluebird');
exports.serviceCall = function(options) {
parameters = {
format: options.format,
stuff: options.stuff
}
returnnewPromise( function( resolve, reject )
{
foo.fooMethod( parameters
function( error, response, body )
{
sails.log("Finished with call");
if ( error )
{
sails.log( error );
throw error;
}
else
{
returnresolve(response.results);
}
});
})
};
};
This can then be called directly as
SomeService.serviceCall(options).then(function(results){
res.json(results);
})
This is the appropriate design pattern for doing these sorts of things with sailsjs and it works wonderfully and without the silliness of continually passing callbacks around! Special thanks to robdubya for the solution!!
Post a Comment for "Javascript/sails.js Controller Waiting For Service To Call Async Function"