js控制器等待服务调用异步函数

js控制器等待服务调用异步函数

本文介绍了使用Javascript / Sails.js控制器等待服务调用异步函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是非常简单的使用asnyc呼叫等待JavaScript函数来完成的,但它似乎是不太清楚应该如何处理控制器调用一个服务,接着调用异步函数。

让我们假设我有一个简单的异步方法:

\r
\r

foo.fooMethod({格式:_format,东东:_stuff},\r
功能(错误,响应,体)\r
{\r
    如果(错误)\r
{\r
sails.log(错误);\r
res.send(错误+错误,500);\r
}\r
\r
res.send(体,200);\r
\r
});

\r

\r
\r

在这种情况下,如果我把它放在控制器回调会导致响应我的异步调用会工作得很好,当回调函数最终控制被发送。但是,如果我把这个变成一个服务,我有一个更有趣的问题。

里面的MyService我定义这个出口为serviceCall

\r
\r

exports.serviceCall =功能(选件){\r
\r
    async.auto(\r
    {\r
        测试:函数(回调)\r
        {\r
            foo.fooMethod({格式:_format,东东:_stuff},\r
                功能(错误,响应,体)\r
            {\r
                sails.log(具有呼叫完成);\r
                \r
                如果(错误)\r
                {\r
                    sails.log(错误);\r
                    回调(错误);\r
                    //res.send(错误+错误,500);\r
                }\r
\r
                回调(NULL,体);\r
\r
                //res.send(体,200)\r
            });\r
        }\r
    },\r
\r
    功能(犯错,结果)\r
    {\r
        sails.log(Returrning);\r
        返回结果;\r
    }\r
    );\r
\r
};

\r

\r
\r

的问题是,如果我把这种从一个控制器,明显的反应。服务调用将立即返回,所以我没有得到来自serviceCall的响应,这样我可以给这个响应用户的方式。因为我不想在node.js的阻止,它不是清除相应的方法是从一个控制器 - >服务 - >异步方法。

叫什么
解决方案

在上#sailsjs与精彩的社区聊天的IRC我发现不是处理过周围回调的远远更好的答案(讨厌这个设计模式)。

\r
\r

VAR无极=要求('蓝鸟');\r
\r
exports.serviceCall =功能(选件){\r
\r
     参数= {\r
       格式:options.format,\r
       东东:options.stuff\r
     }\r
\r
        返回新希望(函数(解析,拒绝)\r
        {\r
            foo.fooMethod(参数\r
                功能(错误,响应,体)\r
            {\r
                sails.log(具有呼叫完成);\r
                \r
                如果(错误)\r
                {\r
                    sails.log(错误);\r
                    抛出的错误;\r
                }\r
                其他\r
                {\r
                    返回解析(response.results);\r
                }\r
\r
            });\r
        })\r
};\r
\r
\r
};

\r

\r
\r

这可以被直接称为

\r
\r

SomeService.serviceCall(选项)。然后(功能(结果){\r
    \r
    res.json(结果);\r
    \r
})

\r

\r
\r

这是做这些事情与sailsjs相应的设计模式,它奇妙的作品,并且没有经过不断回调周围的愚蠢!特别感谢robdubya的解决方案!

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.

解决方案

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

这篇关于使用Javascript / Sails.js控制器等待服务调用异步函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 09:03