|
| 1 | +const scheduleCompatCallback = typeof setImmediate === 'function' |
| 2 | + ? (fn) => setImmediate(fn) |
| 3 | + : (fn) => setTimeout(fn, 0); |
| 4 | + |
| 5 | +function qFinally(onFinally) { |
| 6 | + const handler = typeof onFinally === 'function' ? onFinally : () => onFinally; |
| 7 | + const PromiseCtor = typeof this.constructor === 'function' && typeof this.constructor.resolve === 'function' |
| 8 | + ? this.constructor |
| 9 | + : Promise; |
| 10 | + return this.then( |
| 11 | + (value) => PromiseCtor.resolve(handler()).then(() => value), |
| 12 | + (reason) => PromiseCtor.resolve(handler()).then(() => { |
| 13 | + throw reason; |
| 14 | + }) |
| 15 | + ); |
| 16 | +} |
| 17 | + |
| 18 | +function qFin(handler) { |
| 19 | + return this.finally(handler); |
| 20 | +} |
| 21 | + |
| 22 | +function qDone(onFulfilled, onRejected) { |
| 23 | + this.then(onFulfilled, onRejected).catch((err) => { |
| 24 | + scheduleCompatCallback(() => { |
| 25 | + throw err; |
| 26 | + }); |
| 27 | + }); |
| 28 | +} |
| 29 | + |
| 30 | +function qNodeify(callback) { |
| 31 | + if (typeof callback !== 'function') { |
| 32 | + return this; |
| 33 | + } |
| 34 | + |
| 35 | + this.then( |
| 36 | + (value) => scheduleCompatCallback(() => callback(null, value)), |
| 37 | + (error) => scheduleCompatCallback(() => callback(error)) |
| 38 | + ); |
| 39 | + |
| 40 | + return this; |
| 41 | +} |
| 42 | + |
| 43 | +function qFail(onRejected) { |
| 44 | + return this.catch(onRejected); |
| 45 | +} |
| 46 | + |
| 47 | +function qProgress() { |
| 48 | + return this; |
| 49 | +} |
| 50 | + |
| 51 | +function qSpread(onFulfilled, onRejected) { |
| 52 | + return this.then( |
| 53 | + (values) => { |
| 54 | + if (typeof onFulfilled !== 'function') { |
| 55 | + return values; |
| 56 | + } |
| 57 | + if (Array.isArray(values)) { |
| 58 | + // eslint-disable-next-line prefer-spread |
| 59 | + return onFulfilled.apply(void 0, values); |
| 60 | + } |
| 61 | + return onFulfilled(values); |
| 62 | + }, |
| 63 | + onRejected |
| 64 | + ); |
| 65 | +} |
| 66 | + |
| 67 | +function applyQCompat(promise) { |
| 68 | + if (!promise || (typeof promise !== 'object' && typeof promise !== 'function')) { |
| 69 | + return promise; |
| 70 | + } |
| 71 | + |
| 72 | + if (promise.__cloudinaryQCompatApplied) { |
| 73 | + return promise; |
| 74 | + } |
| 75 | + |
| 76 | + Object.defineProperty(promise, '__cloudinaryQCompatApplied', { |
| 77 | + value: true, |
| 78 | + enumerable: false |
| 79 | + }); |
| 80 | + |
| 81 | + const nativeThen = promise.then; |
| 82 | + if (typeof nativeThen === 'function') { |
| 83 | + promise.then = function (...args) { |
| 84 | + return applyQCompat(nativeThen.apply(this, args)); |
| 85 | + }; |
| 86 | + } |
| 87 | + |
| 88 | + const nativeCatch = promise.catch; |
| 89 | + if (typeof nativeCatch === 'function') { |
| 90 | + promise.catch = function (...args) { |
| 91 | + return applyQCompat(nativeCatch.apply(this, args)); |
| 92 | + }; |
| 93 | + } |
| 94 | + |
| 95 | + const nativeFinally = promise.finally; |
| 96 | + if (typeof nativeFinally === 'function') { |
| 97 | + promise.finally = function (...args) { |
| 98 | + return applyQCompat(nativeFinally.apply(this, args)); |
| 99 | + }; |
| 100 | + } else { |
| 101 | + promise.finally = qFinally; |
| 102 | + } |
| 103 | + |
| 104 | + if (typeof promise.fin !== 'function') { |
| 105 | + promise.fin = qFin; |
| 106 | + } |
| 107 | + |
| 108 | + if (typeof promise.done !== 'function') { |
| 109 | + promise.done = qDone; |
| 110 | + } |
| 111 | + |
| 112 | + if (typeof promise.nodeify !== 'function') { |
| 113 | + promise.nodeify = qNodeify; |
| 114 | + } |
| 115 | + |
| 116 | + if (typeof promise.fail !== 'function') { |
| 117 | + promise.fail = qFail; |
| 118 | + } |
| 119 | + |
| 120 | + if (typeof promise.progress !== 'function') { |
| 121 | + promise.progress = qProgress; |
| 122 | + } |
| 123 | + |
| 124 | + if (typeof promise.spread !== 'function') { |
| 125 | + promise.spread = qSpread; |
| 126 | + } |
| 127 | + |
| 128 | + return promise; |
| 129 | +} |
| 130 | + |
| 131 | +module.exports = applyQCompat; |
0 commit comments