A partire dal 7 febbraio 2018
Chrome 63+, 58+ Firefox e Opera 50+ supporto Promise.finally
.
In Node.js 8.1.4+ (V8 5.8+), la funzione è disponibile dietro la bandiera --harmony-promise-finally
.
Il Promise.prototype.finally ECMAScript Proposal è attualmente in stage 3 del processo TC39.
Nel frattempo per avere promesse.finalmente funzionalità in tutti i browser; è possibile aggiungere un ulteriore then()
dopo il catch()
per richiamare sempre tale richiamata.
Esempio:
myES6Promise.then(() => console.log('Resolved'))
.catch(() => console.log('Failed'))
.then(() => console.log('Always run this'));
JSFiddle Demo: https://jsfiddle.net/moogs/9frfjcsg/
Oppure è possibile estendere il prototipo per includere un metodo finally()
(sconsigliato):
Promise.prototype.finally = function(cb) {
const res =() => this;
const fin =() => Promise.resolve(cb()).then(res);
return this.then(fin, fin);
};
JSFiddle Demo: https://jsfiddle.net/moogs/c67a6ss0/1/
C'è anche la libreria shim Promise.prototype.finally.
fonte
2016-03-14 22:14:50
Proposta ECMAScript (https://github.com/tc39/proposal-promise-finally) e il flag v8 (nodo --harmony_promise_finally app.js) esiste. – masakielastic
Puoi provare il pacchetto 'when' https://www.npmjs.com/package/when – Arno