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('.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);
})
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./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);
})
Share
Improve this question
asked May 22, 2017 at 17:52
JerilJeril
8,5216 gold badges59 silver badges73 bronze badges
3 Answers
Reset to default 9After 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./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 plete 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 plete 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);
Selected answer is not correct anymore.
Nor is superagent's doc, mentionning a vague req
variable which is nowhere defined.
Truth is :
.end()
returnundefined
(since this mit).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 plete the request');
} catch(e) { done(e); }
});
setTimeout(function() {
chain.req.abort();
}, 1000);
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();