Created
June 14, 2014 14:01
-
-
Save fflorent/0fca2eb8a9151b31baef to your computer and use it in GitHub Desktop.
wrapping promise using AOP
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() | |
{ | |
var wrappedPromiseConstructor = Promise; | |
Promise = function(...args) | |
{ | |
// Do whatever you want here | |
console.log("trapped"); | |
var func = args[0]; | |
args[0] = function(resolve, reject) | |
{ | |
setTimeout(function() | |
{ | |
reject("timeout"); | |
}, 1000); | |
func(resolve, reject); | |
}; | |
var promise = new wrappedPromiseConstructor(...args); | |
promise.catch(function(ex) | |
{ | |
console.error("oups, exception : ", ex); | |
throw ex; | |
}); | |
return promise; | |
} | |
})(); | |
// Just a few tests | |
new Promise(function() | |
{ | |
new undefined(); | |
}); | |
new Promise(function(resolve, reject) | |
{ | |
setTimeout(() => resolve(1), 500); | |
}).then((v) => console.log("test 2, value: ", v), | |
(e) => console.error("test 2, error: ", e)); | |
new Promise(function(resolve, reject) | |
{ | |
setTimeout(() => resolve(1), 1500); | |
}).then((v) => console.log("test 3, value: ", v), | |
(e) => console.error("test 3, error: ", e)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment