cujojs / when

A solid, fast Promises/A+ and when() implementation, plus other async goodies.
Other
3.44k stars 396 forks source link

Catch reserved keyword #308

Closed bashilbers closed 10 years ago

bashilbers commented 10 years ago

This is my script:

importFeed().then(function() {
    self.log.info('Importing feed done');
 }).catch(function(e) { });

My developer console says this: Uncaught TypeError: undefined is not a function

catch is on the reserved list: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Reserved_Words

cite: "...may not be used as ... method"

So, how do I keep track of async errors when I cant call catch on my promise?

briancavalier commented 10 years ago

@bashilbers catch is part of the ES6 Promise standard, so it is allowed as a method in ES5+ environments. For ES3 environments, you can use the alias otherwise.

However, "undefined is not a function" sounds like an unrelated error. Are you sure you're calling catch on a when.js promise, and not some other promise or promise-like, such as a jQuery deferred?

unscriptable commented 10 years ago

Hey @bashilbers,

There are two ways you can use catch in older browsers: a) quote it, b) use an alias.

Quote it

importFeed().then(function() {
    self.log.info('Importing feed done');
 })['catch'](function(e) { });

Alias

otherwise() is an alias for catch(). See the docs: https://github.com/cujojs/when/blob/master/docs/api.md#promisecatch

briancavalier commented 10 years ago

That Mozilla page is a bit confusing. The first sentence seems to be outdated or ES3-specific. Have a look at the ES5 section of that Mozilla link for info on ES5 (and later) envs.

bashilbers commented 10 years ago

I'm using otherwise now, which is working for me

briancavalier commented 10 years ago

Cool, glad that worked for you. There are aliases for the other ES3 reserved words as well, eg finally -> ensure.