Added method to poll a tasks status
This commit is contained in:
parent
0bd960d775
commit
876c5df6f5
1 changed files with 34 additions and 0 deletions
34
src/store.js
34
src/store.js
|
|
@ -89,6 +89,40 @@ export default new Vuex.Store({
|
|||
})
|
||||
},
|
||||
|
||||
pollTask(context, [taskId, timeout, interval]) {
|
||||
var endTime = Number(new Date()) + (timeout || 30000);
|
||||
interval = interval || 500;
|
||||
|
||||
var checkCondition = function(resolve, reject) {
|
||||
// If the condition is met, we're done!
|
||||
axios.get(`${context.getters.uri}/task/${taskId}`)
|
||||
.then(response => {
|
||||
console.log(response.data.status)
|
||||
var result = response.data.status;
|
||||
// If the task ends with success
|
||||
if(result == 'success') {
|
||||
resolve(response.data.return);
|
||||
}
|
||||
// If task ends with an error
|
||||
else if (result == 'error') {
|
||||
reject(new Error(response.data.return));
|
||||
}
|
||||
// If the condition isn't met but the timeout hasn't elapsed, go again
|
||||
else if (Number(new Date()) < endTime) {
|
||||
setTimeout(checkCondition, interval, resolve, reject);
|
||||
}
|
||||
// Didn't match and too much time, reject!
|
||||
else {
|
||||
reject(new Error('Polling timed out'));
|
||||
}
|
||||
})
|
||||
|
||||
};
|
||||
|
||||
return new Promise(checkCondition);
|
||||
|
||||
},
|
||||
|
||||
handleHTTPError(context, error) {
|
||||
var errormsg = '';
|
||||
if (error.response) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue