Write a function `promiseFinally(promise, onFinally)` that calls `onFinally` regardless of whether the promise resolves or rejects.
Problem Statement
Write a function `promiseFinally(promise, onFinally)` that calls `onFinally` regardless of whether the promise resolves or rejects.
Complexity
Time Complexity: -
Space Complexity: -
Hints
š” Hint 1:
finally runs the callback whether the promise fulfills or rejects.
š” Hint 2:
Chain .then() for both success and failure, calling onFinally in both.
š” Hint 3:
promise.then(val => { onFinally(); return val; }, err => { onFinally(); throw err; });
ā
Solution:
```javascript
function promiseFinally(promise, onFinally) {
return promise.then(
val => { onFinally(); return val; },
err => { onFinally(); throw err; }
);
}
```