Reputation: 8531
I am trying to calculate the progress of a file upload, so I was using Superagent. I was able to get the progress of the file upload.
Now when a user selects the cancel button, I need to interrupt or cancel the post request. Is there any way to do that. The following is my code:
var file = somefile.pdf
Request.post('http://posttestserver.com/post.php?dir=example')
.set("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
.send(file)
.on('progress', function(e) {
console.log('Progress', e.percent);
})
.end((err, res) => {
console.log(err);
console.log(res);
})
Upvotes: 4
Views: 4770
Reputation: 516
Trying to figure out how to get cancel working with promises I did the following:
var req = request.get('/');
req.then((response) => {
// do work
}).catch((error) => {});
req.xhr.abort();
Upvotes: 1
Reputation: 550
Selected answer is not correct anymore.
Nor is superagent's doc, mentionning a vague req
variable which is nowhere defined.
Truth is :
.end()
return undefined
(since this commit).then()
returns a Promise.end()
is more or less deprecated and its documentation is wrong:
/*
* Initiate request, invoking callback `fn(res)`
* with an instanceof `Response`.
*
* @param {Function} fn
* @return {Request} for chaining
* @api public
*/
Request.prototype.end = function (fn) {
if (this._endCalled) {
console.warn("Warning: .end() was called twice. This is not supported in superagent");
}
this._endCalled = true;
// store callback
this._callback = fn || noop;
// querystring
this._finalizeQueryString();
this._end();
};
Solution:
var chain = request.get(uri + '/delay/3000');
chain.end(function(err, res){
try {
assert(false, 'should not complete the request');
} catch(e) { done(e); }
});
setTimeout(function() {
chain.req.abort();
}, 1000);
Upvotes: 6
Reputation: 8531
After some research I was able to find out how to interrupt
or cancel
or abort
a superagent request. The following code will work:
var file = somefile.pdf
var req = Request.post('http://posttestserver.com/post.php?dir=example')
.set("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
.send(file)
.on('progress', function(e) {
if (condition) {
req.abort() // to cancel the request
}
console.log('Progress', e.percent);
})
.end((err, res) => {
console.log(err);
console.log(res);
})
Additional information from their docs
.abort()
should abort the request
var req = request
.get(uri + '/delay/3000')
.end(function(err, res){
try {
assert(false, 'should not complete the request');
} catch(e) { done(e); }
});
req.on('error', function(error){
done(error);
});
req.on('abort', done);
setTimeout(function() {
req.abort();
}, 500);
should allow chaining .abort() several times
var req = request
.get(uri + '/delay/3000')
.end(function(err, res){
try {
assert(false, 'should not complete the request');
} catch(e) { done(e); }
});
// This also verifies only a single 'done' event is emitted
req.on('abort', done);
setTimeout(function() {
req.abort().abort().abort();
}, 1000);
Upvotes: 9