Fetch: is there an "always" function?

Created on 17 Oct 2015  路  5Comments  路  Source: github/fetch

I want to execute when either a .then or a .catch is called. Typically this is handled with a .always

Most helpful comment

var done = function(response) { console.log('response', response) }
var fail = function(error) { console.log('error', error) }
var always = function() { console.log('always') }
Promise.resolve(42).then(done).catch(fail).then(always, always)

All 5 comments

var done = function(response) { console.log('response', response) }
var fail = function(error) { console.log('error', error) }
var always = function() { console.log('always') }
Promise.resolve(42).then(done).catch(fail).then(always, always)

@chovy Unfortunately, Promises don't have an .always() method. You need to attach an always handler as both resolve and error handler to cover both cases. Clumsy, I know!

So how do we execute an always call?

@rustanacexd: See @dgraham's code above.

I did this:

class Platform {
  static init() {
    if(!Promise.prototype.finally) {//.finally is not supported by promise for iOS: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/finally
      Promise.prototype.finally = function(callback) {
        return this.then(callback)
          .catch(callback);
      };
    }
  }
}

and just called Platform.init() from the application, now I can use .finally for promises as normal on iOS :).

Was this page helpful?
0 / 5 - 0 ratings

Related issues

javan picture javan  路  3Comments

DimitryDushkin picture DimitryDushkin  路  4Comments

karladler picture karladler  路  4Comments

proofrock picture proofrock  路  3Comments

AllenFang picture AllenFang  路  5Comments