I was using this ES6 Promise compatible finally implementation called promise.prototype.finally in a Node application that I want to convert to TypeScript however there are no typing available for this package that I can find on DefinitelyTyped. In these situations I've written up my own impromptu type definitions for just the subset of functionality that I need but in this case it is a library that modifies the prototype of the Promise object and I haven't encountered any conventional way to represent this in TypeScript. Any ideas?
Possibly related:
The finally() method of a Promise schedules a function, the callback function, to be called when the promise is settled. Like then() and catch() , it immediately returns an equivalent Promise object, allowing you to chain calls to another promise method, an operation called composition.
As an extra step to handle errors, Javascript and Typescript provide an additional “finally”-keyword. Such a “finally”-block executes any code within its scope after both the “try”-block as well as the “catch”-block have executed (if an error happened). // This function is absolutely save // to call.
finally() method to the standard library. The finally() method lets you execute a callback function once the promise that it's called on has been settled (that is, it has been either fulfilled or rejected and is therefore no longer pending): somePromise. finally(() => { // Code });
The finally() method schedule a function to execute when the promise is settled, either fulfilled or rejected.
Whilst the answer from Slava is correct, it only deals with the typing of the finally
block. To actually incorporate the shim into your code, so you can write p.finally(() => { ... })
, you need to invoke the shim.
Unfortunately the typings on DefinitelyTyped do not currently support the shim
function so until this is supported, I'd advise to add the typings yourself.
declare interface Promise<T> {
finally<U>(onFinally?: () => U | Promise<U>): Promise<U>;
}
declare module 'promise.prototype.finally' {
export function shim(): void;
}
The types are now available. Install with
npm install --save-dev @types/promise.prototype.finally
In your TypeScript code, during application bootstrap, you can then call
import { shim } from 'promise.prototype.finally';
shim();
This will add the finally
block to the Promise
prototype, allowing you to use finally
as required.
For anyone wondering how to get this working natively without any shims: as of TS 2.7 it's possible.
Note that TS 2.7 is not yet fully (26. Feb. 2018) ES2018 compatible. While there are still a few things missing, Promise.finally made it into the 2.7 release. Also tsconfig-schema should already accept ES2018 as target but yet TS 2.7 has no knowledge of ES2018. For now to use the new features, such as Promise.finally, which already are in 2.7, you'll have to use "target": "ESNEXT"
in your tsconfig.json.
Then you'll be able to write code like this:
myAsyncFunction().then
(
(var) => console.log("executing then. " + var)
)
.catch
(
(var) => console.log("executing catch. " + var)
)
.finally
(
() => console.log("executing finally.")
)
Notice how finally will not take any arguments due to it's nature.
IMPORTANT: while TS will transpile correctly and understand what you're doing you still have to check if your JS-Engine supports Promise.finally. In my case I was using NodeJs 8.x and of course the produced JS was not executable because Promise.Finally is supported starting with in the latest NodeJs 10.x nightly builds Node 10.x (stable), see this link.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With