Javascript/Sails.js Controller waiting for service to call async function

痞子三分冷 提交于 2019-12-12 04:51:29

问题


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 Service which in turn calls an async function.

Let's assume that I have a simple asynchronous method:

foo.fooMethod( {format: _format, stuff: _stuff },
function( error, response, body )
{
    if ( error )
	{
	    sails.log( error );
		res.send( "Error " + error, 500 );
	}

	res.send( body, 200 );

});

In this scenario my async call will work just fine if I put it in the controller as the callback will result in the response being sent when the function callback is eventually controlled. However if I put this into a service I have a more interesting problem.

Inside of MyService I define this export for serviceCall

exports.serviceCall = function(options) {

    async.auto(
    {
        test: function( callback )
        {
            foo.fooMethod( {format: _format, stuff: _stuff },
                function( error, response, body )
            {
                sails.log("Finished with call");
                
                if ( error )
                {
                    sails.log( error );
                    callback( error );
                    //res.send( "Error " + error, 500 );
                }

                callback( null, body );

                //res.send( body, 200 )
            });
        }
    },

    function(err, result)
    {
        sails.log("Returrning");
        return result;
    }
    );

};

The problem is that if I call this from a controller, the obvious happens. The Service call will immediately return, so I have no way of getting the response from the serviceCall so that I can send this response to the user. Since I don't want to block in node.js, its not clear what the appropriate way is to call from a Controller->Service->asynchronous method.


回答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).

var Promise = require('bluebird');

exports.serviceCall = function(options) {

     parameters = {
       format: options.format, 
       stuff: options.stuff
     }

        return new Promise( function( resolve, reject )
        {
            foo.fooMethod( parameters
                function( error, response, body )
            {
                sails.log("Finished with call");
                
                if ( error )
                {
                    sails.log( error );
                    throw error;
                }
                else
                {
                    return resolve(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!!



来源:https://stackoverflow.com/questions/27886627/javascript-sails-js-controller-waiting-for-service-to-call-async-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!