使用 JavaScript 返回数据
2014-06-17
96
我在调用 JavaScript 函数时遇到问题,我无法返回我的缓冲区,因为它在其他函数中
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
2个回答
使用 JavaScript 回调模式
您不会在从
callback
函数返回结果时获得结果,这没有任何意义,也不会起作用,因为您对服务器的请求是异步的。请查看下面的示例,了解其工作原理:
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);
}
});
}
现在您要做的只是实现一个回调,该回调在结果完成时执行。
var client = require('./sender');
//envoyer le paquet au seveur blizzard.com
client.sendAndreceive(bufenc, function(received) {
console.log('Received', received);
});
这种 JavaScript 模式称为回调模式,在这种情况下非常有用。
使用承诺
另一种更性感的方式是通过 承诺 。我不想完全解释什么是承诺或承诺的作用(您可以使用搜索,对吧?),所以我只向您展示一个例子:
// 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);
}
)
;
Joseph Callaars
2014-06-17
您正在调用
sendAndReceive()
,
sendandReceive()
正在启动HTTP请求,该请求异步调用
callback()
。因此,
callback()
的返回值是http请求对象中给出的调用者的返回值,而
sendandReceive()
nothent。
没有办法强迫异步呼叫的行为像一个逐步流一样。因此,我支持男孩的建议,使用回调。
好吧,我看到男孩的承诺模式:-) cool。我会更深入地看。看来您可以至少看起来像
。然后()
等构造的异步步骤。 thx!
peter_the_oak
2014-06-17