lightsofapollo/superagent-promise

pipe usage

Closed this issue · 3 comments

How can i accomplish this block of code with superagent-promise:

var request = require('superagent');
request
  .get(url)
  .pipe(fs.createWriteStream(outputFilename))
  .on('finish', function() {
    console.log('Wrote ' + outputFilename);
  });

Have you tried that exact block of code?

But from my point of view, you wouldn't. If you want to pipe the data just use superagent directly. The point of superagent-promise is to avoid the ugliness of the callbacks in end or send methods of superagent.

What is your use case for doing it this way?

I found myself having to implement this recently. If pipe did return a promise I would use it.

The way I would like it to work is for the promise from pipe to resolve with the response after the finish event was called.

I would also make it clear that the response will only have headers (no body) and that it won't reject on a status code > 399. This is just the way superagent works but it would be nice to have it as a note.

request.get(url)
    .pipe(stream)
    .then((response) => {
        // handle response headers/statusCode
    }, (error) => {
        // handle error
    });

Was this meant to be closed?