in Education by
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); }) JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
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);

Related questions

0 votes
    Hi I have to pass array as props to a functional component. Show code snippet But it is converted ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    Hi I have to pass array as props to a functional component. Show code snippet But it is converted ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 6, 2022 in Education by JackTerrance
0 votes
    Hi I have to pass array as props to a functional component. Show code snippet But it is converted ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 3, 2022 in Education by JackTerrance
0 votes
    I'm a bit surprised I'm having trouble finding this online, but I can't seem to find an example ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 7, 2022 in Education by JackTerrance
0 votes
    I'm a bit surprised I'm having trouble finding this online, but I can't seem to find an example ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 7, 2022 in Education by JackTerrance
0 votes
    This question already has answers here: useState set method not reflecting change immediately (17 answers) Closed ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 7, 2022 in Education by JackTerrance
0 votes
    I'm a bit surprised I'm having trouble finding this online, but I can't seem to find an example ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 7, 2022 in Education by JackTerrance
0 votes
    This question already has answers here: useState set method not reflecting change immediately (17 answers) Closed ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 7, 2022 in Education by JackTerrance
0 votes
    This is my object. values : { title : 'this is title ..', translate : { en : 'some texts ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 3, 2022 in Education by JackTerrance
0 votes
    This is my object. values : { title : 'this is title ..', translate : { en : 'some texts ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 3, 2022 in Education by JackTerrance
0 votes
    @RequestMapping annotation is used to map a HTTP request method (GET or POST) to a specific class or method in the ... which will handle the respective request. A. True B. False...
asked Nov 8, 2022 in Education by JackTerrance
0 votes
    I want to get the search result using scrapy post request after giving the input to CP Number as 16308 ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 1, 2022 in Education by JackTerrance
0 votes
    Is it possible to make a timeout of 3 seconds in a post request ? How ? My code for the moment ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 22, 2022 in Education by JackTerrance
0 votes
    This is a cURL function that can send or retrieve data. It should work with any PHP app that ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 4, 2022 in Education by JackTerrance
0 votes
    I am new to jmeter. I have been experimenting on creating a jmeter script manually by inspecting the ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 1, 2022 in Education by JackTerrance
...