用JavaScript返回数据
我有一个问题,当我打电话给我的函数JavaScript我不能返回我的缓冲区,因为它在其他function
index.js:
var client = require('./sender'); //envoyer le paquet au seveur blizzard.com var received =client.sendAndreceive(bufenc); console.log('received: '+received);
sender.js:
var Http = require('http'); var Url = require('url'); function sendAndreceive(data) { console.log('##########################################################################'); // connexion au serveur var options = { //proxy host: "proxya.u-pec.fr", port: 3128, //hostname : 'http://m.eu.mobileservice.blizzard.com', //port : 80, path : 'http://m.eu.mobileservice.blizzard.com/enrollment/enroll.htm', method : 'POST', headers: { Host: "http://m.eu.mobileservice.blizzard.com" } }; var req = Http.request(options, callback ); req.on('error', function(e) { console.log('problem with request: ' + e.message); }); req.write(data); req.end(); } exports.sendAndreceive = sendAndreceive; function callback (res) { res.on('data', function(chunk) { buf = new Buffer(chunk); console.log('data length: ' + buf.length); return buf; // my problem is her !!!!!!!!!!!!!!!!!!!!!! }); }
输出:
########################################################################## received: undefined data length: 45
使用JavaScriptcallback模式
当你从callback
函数返回结果时,你不会得到结果,这是没有用的,因为你对服务器的请求是asynchronous的。 看看下面的例子,看看它是如何工作的:
function callback (res, callback) { res.on('data', function(chunk) { buf = new Buffer(chunk); console.log('data length: ' + buf.length); // Make sure the argument is indeed a callable function if (typeof callback === 'function') { callback(buf); } }); }
现在你所做的只是实现一个callback,当结果完成时执行。
var client = require('./sender'); //envoyer le paquet au seveur blizzard.com client.sendAndreceive(bufenc, function(received) { console.log('Received', received); });
这个JavaScript模式被称为callback模式,在这种情况下可以非常有用。
使用承诺
另一种更性感的做法是通过承诺 。 我不想完全解释一个承诺是什么或者做什么(你可以使用search,对吗?),所以我只是给你一个例子:
// This is part of the node-promise module: https://github.com/kriszyp/node-promise var deferred = require("promise").defer; var client = require('./sender'); function callback (res) { // Create a deffered object that will be returned var deferred = defer(); res.on('data', function(chunk) { buf = new Buffer(chunk); console.log('data length: ' + buf.length); // Now let the listeners know that this promise // has been completed successfully, and return the buf deffered.resolve(buf); // Or if something went wrong, you could do the following: // deffered.reject('Everything is wrong!'); }); return deferred; } //envoyer le paquet au seveur blizzard.com client.sendAndreceive(bufenc) .then( // Function ended successfully function(received) { console.log('Received', received); }, // Function returned an error function(err) { console.log('Oh noes!', err); } ) ;
您正在调用sendAndreceive()
,并且sendAndreceive()
正在启动一个asynchronous调用callback()
的HTTP请求。 所以callback()
的返回值是给HTTP请求对象内的调用者的返回值,而sendAndreceive()
什么都不返回。
没有办法强制asynchronous调用的行为像一步一步的stream程。 因此,我支持男孩的build议,使用callback。
好吧,我看到男孩的承诺模式:-)很酷。 我会深入了解一下。 看起来你可以使asynchronous步骤至less看起来像使用.then()
这样的结构同步写入。 谢谢!