用promise封装http.get获取网络数据流

function startRequest(url) {
return new Promise((resolve, reject) => {
http.get(url, function (res) {
var chunks = []; //用于保存网络请求不断加载传输的缓冲数据
var size = 0;   //保存缓冲数据的总长度
res.on(‘data’, function (chunk) {
chunks.push(chunk);
size += chunk.length;
});
res.on(‘end’, function (res) {
var data = Buffer.concat(chunks, size);
resolve(data)
});
});
})
}

你可能感兴趣的:(用promise封装http.get获取网络数据流)