Last active
August 29, 2015 13:56
-
-
Save delapuente/9055624 to your computer and use it in GitHub Desktop.
Mock promise supported interface to produce delayed values
This file contains 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 fPromise(timeout, result, error) { | |
return new Promise(function(fulfill, reject) { | |
setTimeout(function () { | |
if (!error) fulfill(result); | |
else if (error instanceof Error) throw error; | |
else reject(error); | |
}, timeout); | |
}); | |
} | |
/* | |
How to use: | |
- Fulfilled promise: fPromise(1000, 'ok-result'); | |
- Rejected promise: fPromise(1000, null, 'error-result'); | |
- Throwing promise: fPromise(1000, null, new Error('message')); | |
*/ |
Yep. Notice the gist try to simulate an async implementation throwing an Error, not a handler throwing an error. Nothing more.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
doing a throw in other event loop cycle, will never get caught by promises. (Unless you are using zone.js or nodejs domains)
This should work