Skip to content

Instantly share code, notes, and snippets.

@darolo
Created November 15, 2019 13:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save darolo/c8dda5e36816a8899f0e2eff6bb07978 to your computer and use it in GitHub Desktop.
Save darolo/c8dda5e36816a8899f0e2eff6bb07978 to your computer and use it in GitHub Desktop.
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