Use finally on promise

20.04.2017Stefan Welsch
Mobile Ionic Angular Good-to-know

By default, there is no Finally block for a Promise. To use a Finally block with your promise, you can use the Shim promise.prototype.finally.

  1. Install * promise.prototype.finally *
1
$ npm install --save promise.prototype.finally
  1. Install the typing
1
$ npm install --save-dev @types/promise.prototype.finally
  1. Import the shim into your * app.module.ts * and initialize it.
1
2
import { shim } from 'promise.prototype.finally';
shim();
  1. You can now use the Finally block as follows.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
function().then(response => {

    // do something

  }).catch(error => {

    // do error handling

  }).finally(() => {

    // do it in every case

  }
);

This text was automatically translated with our golang markdown translator.

Stefan Welsch

Stefan Welsch – Pionier, Stuntman, Mentor. Als Gründer von b-nova ist Stefan immer auf der Suche nach neuen und vielversprechenden Entwicklungsfeldern. Er ist durch und durch Pragmatiker und schreibt daher auch am liebsten Beiträge die sich möglichst nahe an 'real-world' Szenarien anlehnen.