So, I wanted to expand Promise to have “progress” so that I could report it using Promise for my Async tasks.
So I distributed Promise as follows:
class promisePro extends Promise {
constructor(fn) {
super(function (resolve, reject) {
fn(resolve, reject, this._progress.bind(this));
});
}
_progress(v) {
if (this.progressCB)
this.progressCB(v);
}
progress(fn) {
this.progressCB = fn;
}
}
and used it:
function ptest() {
return new promisePro((resolve, reject, progress) => {
setTimeout(() => {
progress(0.3)
}, 1000)
setTimeout(() => {
progress(0.6)
}, 2000)
setTimeout(() => {
progress(0.9)
}, 3000)
setTimeout(() => {
resolve(1)
}, 4000)
})
}
and used itt:
ptest().then((r) => {
console.log('finiished: ' + r)
}).progress((p) => {
console.log('progress: ' + p)
})
and got this error:
ptest().then((r) => {
^
TypeError: Promise resolve or reject function is not callable
What am I doing wrong here?
I used node 7.5 upgraded to 8.4. Got this error in both versions.
Thank.
source
share