Skip to content

Instantly share code, notes, and snippets.

@elycruz
Last active October 30, 2015 00:19
Show Gist options
  • Save elycruz/1fa9e85332b61802adcf to your computer and use it in GitHub Desktop.
Save elycruz/1fa9e85332b61802adcf to your computer and use it in GitHub Desktop.
function _process(value) {
return new Promise(function (fulfill, reject) {
var count = 0, timeout;
timeout = setInterval(function () {
console.log('iteration ' + count);
if (count > 5 && value !== 'fail') { fulfill(true); clearInterval(timeout); }
else if (count > 5 && value === 'fail') { reject('Operation timed out.'); clearInterval(timeout); }
count += 1;
}, 100);
});
}
// Then to test it (let's say in `console`) try something line:
var a = _process();
a
.then(function (value) {
console.log('Operation completed successfully. Value recieved: ' + value);
})
.catch(function (reason) {
console.log(reason);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment