Skip to content

Instantly share code, notes, and snippets.

@bilalq
Created May 17, 2014 09:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bilalq/6143ebdeee14b7a42057 to your computer and use it in GitHub Desktop.
Save bilalq/6143ebdeee14b7a42057 to your computer and use it in GitHub Desktop.
Sweet $q Promise methods for IE8
/**
* 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!');
});
@bilalq
Copy link
Author

bilalq commented May 17, 2014

This still has problems when a .then is inserted in between the .catch and .finally in the chain.

This:

promise.catch(function(err) {
  console.log(err);
})
.then(function(res) {
  return res.data;
})
.finally(function(){
  console.log('Finally!');
});

ends up compiling to:

promise['catch'](function (err) {
    console.log(err);
}.then(function (res) {
    return res.data;
}))['finally'](function () {
    console.log('Finally!');
});

I've tried messing around with different levels of precedence, but to no avail.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment