espruino / Espruino

The Espruino JavaScript interpreter - Official Repo
http://www.espruino.com/
Other
2.76k stars 743 forks source link

Add Promise.prototype.finally #2428

Open mariusGundersen opened 10 months ago

mariusGundersen commented 10 months ago

It seems like finally is missing from Promise prototype. It should be fairly easy to implement as

Promise.prototype.finally = function (f) {
  return this.then(
    (x) => Promise.resolve(f()).then(() => x),
    (x) => Promise.resolve(f()).then(() => Promise.reject(x))
  );
};

finally is called if the original promise resolves or rejects, and does not affect what is passed on in the chain, unless the finally callback throws/rejects.

BTW, there is still a problem with the promise chain, see #2227