Last active
October 30, 2015 00:19
-
-
Save elycruz/1fa9e85332b61802adcf 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 _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