-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.js
34 lines (29 loc) · 1.08 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
var axios = require('axios');
var cancel = require('../cancel');
//set custom adapter as default for all axios calls
axios.defaults.adapter = require('../xhr');
var button = document.querySelector('.load');
var cancellation;
function finishRequest(status) {
cancellation = null;
button.innerText = 'Make request'
document.querySelector('.status').innerHTML = status;
}
button.addEventListener('click', function() {
if(!cancellation) {
button.innerText = 'Cancel'
document.querySelector('.status').innerHTML = 'Loading...'
//put cancellation token as request option
cancellation = new cancel.Cancellation()
axios('/api', {cancellation: cancellation}).then(function() {
finishRequest('Success')
}, function(error) {
//cancellation can be handled there as special error object
finishRequest(error instanceof cancel.CancellationError ? 'Cancelled' : 'Error');
})
} else {
//cancel token if we have some unresolved
cancellation.cancel();
finishRequest();
}
});