lundi 1 septembre 2014

Node streams - cannot pipe after data has been emitted (request + promises)


Vote count:

0




Ok, this has a lot of moving parts, promises, request and streams.


I'm trying to wrap the request module with a promise, and be able to resolve it either with data, or a stream. When I try to get a stream, it says You cannot pipe after data has been emitted from the response



function download(url, toStream)
{
return new Promise(resolve, reject)
{
var req = request(url);

req.on('response', function (res)
{
var status = res.statusCode.toString();
if (status[0] == '4' || status[0] == '5')
{
return reject('bad status');
}

if (toStream)
{
//return the pipe
return resolve(req);
}
var allData;
//if not streaming - use the data events to read the response and resolve later
res.on('data', function (chunk) { ... }

res.on('end', function () {
resolve(allData);
});
});
}
}


I run it like this: var streamPromise = download(url, true);



asked 1 min ago

Madd0g

474






Node streams - cannot pipe after data has been emitted (request + promises)

Aucun commentaire:

Enregistrer un commentaire