Why doesn't Promise.prototype.finally() receive any arguments?
The documentation says:
A finally callback will not receive any argument, since there's no reliable means of determining if the promise was fulfilled or rejected. This use case is for precisely when you do not care about the rejection reason, or the fulfillment value, and so there's no need to provide it
However, i can add a simple method to the Promise prototype, lets name it finally2 (Promise.prototype.finally2
) which can receive the result, and can reliably determine if the promise was fulfilled or rejected.
Promise.prototype.finally2 = function(callback){
return this.then(result => callback(result), result => callback(undefined, result));
}
I understand (as the documentation says) that the use case for Promise.prototype.finally
is precisely when you do not care about the rejection reason, or the fulfillment value.
My question is: How is it unreliable to determine if the promise was fulfilled or rejected in Promise.prototype.finally
?
Promise.prototype.finally2 = function(callback){
return this.then(result => callback(result), result => callback(undefined, result));
}
Promise.resolve(2).finally((...args) => console.log('finally:resolve =>', args));
Promise.reject(2).finally((...args) => console.log('finally:reject =>', args));
Promise.resolve(2).finally2((...args) => console.log('finally2:resolve =>', args));
Promise.reject(2).finally2((...args) => console.log('finally2:reject =>', args));