Created
May 17, 2014 09:45
-
-
Save bilalq/6143ebdeee14b7a42057 to your computer and use it in GitHub Desktop.
Sweet $q Promise methods for IE8
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
/** | |
* Macros to make $q Promises better in IE8 | |
* | |
* Tested with v0.6.0 of sweet.js | |
*/ | |
/** | |
* These operator macros will take `.catch` and `.finally` method invocations | |
* and transform them into IE8 safe variants. | |
*/ | |
operator (.catch) 1 left | |
{ $base, $exp } => #{ $base['catch']($exp) } | |
operator (.finally) 1 left | |
{ $base, $exp } => #{ $base['finally']($exp) } | |
/** | |
* Example code | |
*/ | |
promise | |
.then(function(res) { | |
return res.data; | |
}) | |
.catch(function(err) { | |
console.log(err); | |
}) | |
.finally(function(){ | |
console.log('Finally!'); | |
}); | |
/** | |
* Transforms to this | |
*/ | |
promise.then(function (res) { | |
return res.data; | |
})['catch'](function (err) { | |
console.log(err); | |
})['finally'](function () { | |
console.log('Finally!'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This still has problems when a
.then
is inserted in between the.catch
and.finally
in the chain.This:
ends up compiling to:
I've tried messing around with different levels of precedence, but to no avail.