SPLIT INTO MULTIPLE PAGES
Promise Extensions
promise.tap(fulfilled?: Function , rejected?: Function) => Promise
- A pass-through that gives access to the current value
//Warning: Modifies the Promise prototype
Promise.prototype.tap = function (onFulfilled, onRejected) {
return this.then(
(result) =>
onFulfilled
? Promise.resolve(onFulfilled(result))
.then(() => result)
: Promise.resolve(result)
,
(reason) => {
onRejected
? Promise.resolve(onRejected(reason))
.then(() => Promise.reject(reason))
: Promise.reject(reason)
}
)
}
const yell = (words) => console.log(`${words.toUpperCase()}!!!`)
const log = (value) => console.log(value)
const logError = (reason) => console.error(reason)
Promise.resolve('Roar!').tap(yell).then(log)
Promise.reject(new Error('Timed Out')).tap(log, logError)
promise.always(fulfilled?: Function , rejected?: Function) => Promise
- Do something on settled. (fulfilled or rejected)
//Warning: Modifies the Promise prototype
Promise.prototype.always = function (onSettled) {
return this.then(
(value) => Promise.resolve(onSettled())
.then(() => value),
(reason) => onSettled()
.then(() => Promise.reject(reason)))
}
takeALongTimeToLoad().always(hideLoadingIndicator)