Created
November 15, 2019 13:15
-
-
Save darolo/c8dda5e36816a8899f0e2eff6bb07978 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
getPromiseWithProgress(resolver){ | |
return new (function PromiseWithProgress(){ | |
let observers = [], | |
blockNotify = false, | |
notify = data => !blockNotify && observers.forEach(observer => observer(data)), | |
promise = new Promise((resolve, reject) => resolver(resolve, reject, notify)) | |
this.progress = function(observer){ typeof observer === 'function' && observers.push(observer); return this } | |
this.then = function(){ promise.then(...arguments); return this } | |
this.catch = function(){ promise.catch(...arguments); return this } | |
this.finally = function(){ promise.finally(...arguments); return this } | |
promise.finally( () => blockNotify = true ) | |
})() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment