Skip to content

Instantly share code, notes, and snippets.

@fflorent
Created June 14, 2014 14:01
Show Gist options
  • Save fflorent/0fca2eb8a9151b31baef to your computer and use it in GitHub Desktop.
Save fflorent/0fca2eb8a9151b31baef to your computer and use it in GitHub Desktop.
wrapping promise using AOP
(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