Javascript/Sails.js Controller waiting for service

2019-09-15 05:16发布

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条回答
Ridiculous、
2楼-- · 2019-09-15 05:57

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!!

查看更多
登录 后发表回答