Created
April 7, 2020 13:18
-
-
Save singhkumarhemant/41b4201617a3aa2cb733d07859f663cf to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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