Skip to content

Instantly share code, notes, and snippets.

@singhkumarhemant
Created April 7, 2020 13:18
Show Gist options
  • Select an option

  • Save singhkumarhemant/41b4201617a3aa2cb733d07859f663cf to your computer and use it in GitHub Desktop.

Select an option

Save singhkumarhemant/41b4201617a3aa2cb733d07859f663cf to your computer and use it in GitHub Desktop.
function delay(t) {
return new Promise(function(resolve) {
setTimeout(resolve, t);
});
}
work.create()
.then(work.publish) //remote work submission
.then(work.requestStatus)
.then(function() {
// retry until done
var timeout = 10 * 1000;
var start = Date.now();
function check() {
var now = Date.now();
if (now - start > timeout) {
return Promise.reject(new Error("checkStatus() timeout"));
}
return work.requestStatus().then(function(result) {
switch(result.status) {
case "success":
return result; // resolve
case "failure":
throw result; // reject
case default:
case "inProgress": //check every second
return delay(1000).then(check);
}
});
}
return check();
}).then(function(){console.log("work published"})
.catch(console.error);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment