Implement a Custom Polyfill for Promise.finally()

Build a polyfill for the Promise.prototype.finally() method. It should attach a callback that runs regardless of whether the promise fulfilled or rejected, without altering the resolved value or rejection reason. The callback receives no arguments and the original promise's outcome is propagated through. Handle the edge case where the finally callback itself returns a rejected promise or throws.

Examples
Input: []
Output:
Hints

Implement a Custom Polyfill for Promise.finally()

Build a polyfill for the Promise.prototype.finally() method. It should attach a callback that runs regardless of whether the promise fulfilled or rejected, without altering the resolved value or rejection reason. The callback receives no arguments and the original promise's outcome is propagated through. Handle the edge case where the finally callback itself returns a rejected promise or throws.