Use finally on promise

20.04.2017 Stefan Welsch
Mobile Tech ionic angular goodtoknow

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 *
$ npm install --save promise.prototype.finally
  1. Install the typing
$ npm install --save-dev @types/promise.prototype.finally
  1. Import the shim into your * app.module.ts * and initialize it.
import { shim } from 'promise.prototype.finally';
shim();
  1. You can now use the Finally block as follows.
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 - pioneer, stuntman, mentor. As the founder of b-nova, Stefan is always looking for new and promising fields of development. He is a pragmatist through and through and therefore prefers to write articles that are as close as possible to real-world scenarios.