question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

include proposed `Promise.prototype.finally`

See original GitHub issue

basically just:

/**
  `finally` will be invoked regardless of the promise's fate just as native
  try/catch/finally behaves
  Synchronous example:

  findAuthor() {
    if (Math.random() > 0.5) {
      throw new Error();
    }
    return new Author();
  }
  try {
    return findAuthor(); // succeed or fail
  } catch(error) {
    return findOtherAuther();
  } finally {
    // always runs
    // doesn't affect the return value
  }

  Asynchronous example:

  findAuthor().catch(function(reason){
    return findOtherAuther();
  }).finally(function(){
    // author was either found, or not
  });

  @method finally
  @param {Function} callback
  @return {Promise}
*/
  finally(callback) {
    let promise = this;
    let constructor = promise.constructor;

    return promise.then(value => constructor.resolve(callback()).then(() => value),
                  reason => constructor.resolve(callback()).then(() => { throw reason; }));
  }

Questions:

  • should 4.0.0’s es6-promise/auto polyfill the whole promise, or just finally if finally is not present?
  • should we wait a-bit longer or is the risk sufficiently low to just :shipit: cc @domenic

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:2
  • Comments:13 (5 by maintainers)

github_iconTop GitHub Comments

5reactions
ljharbcommented, Mar 12, 2018

@shirakaba this isn’t a future spec feature; finally is stage 4 - it’s already in the language (in ES2018).

In other words, if the core TS library lacks a definition for finally, that’s a bug in TS.

2reactions
shirakabacommented, Mar 13, 2018

Okay, it looks like it may potentially even be a WebStorm IDE issue – Typescript has a lib called lib.esnext.promise.d.ts – importable via --lib esnext.promise, but WebStorm’s language service does not have this library file, so I suspect I’d be unable to use it without issue.

WebStorm’s language service has most of the TypeScript core libs, but also lacks lib.es2018.d.ts. Their lib.esnext.full.d.ts, like TypeScript’s official one, does not include finally(), either. I’ll have to look into it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Promise.prototype.finally() - JavaScript - MDN Web Docs
The finally() method of a Promise object schedules a function to be called when the promise is settled (either fulfilled or rejected).
Read more >
promise.prototype.finally
This package implements the es-shim API interface. It works in an ES3-supported environment that has Promise available globally, and complies ...
Read more >
tc39/proposal-promise-finally
Many promise libraries have a "finally" method, for registering a callback to be invoked when a promise is settled (either fulfilled, or rejected)....
Read more >
Finally the Promise.prototype.finally() is available
Availability of Promise.prototype.finally() brings it to all the Promise-based API, like the global fetch() (used in the demos) and all ...
Read more >
ES2018: `Promise.prototype.finally()`
The proposal “Promise.prototype.finally” by Jordan Harband is at stage 4. This blog post explains it.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found