Answer the question
In order to leave comments, you need to log in
How to track the progress of a Promise?
Good evening!
There is a rather long promise (20+ minutes) that I would like to keep track of. As I understand it, #progress has been cut out of A+ now. How can I track the progress of a promise, preferably in a chain?
I think passing it to #then is also not the best option.
PS Do not offer the Q library , it is slow and does not correspond to half of the standard.
Answer the question
In order to leave comments, you need to log in
Progress was not just removed from Promise, in most cases it is an anti-pattern.
You need to understand that Promise A + are designed to encapsulate all the logic associated with the operation inside them.
That is why I do not advise you to come up with code that requires the Promise.progress, Promise.cancel methods or the Promise.isFullfilled, Promise.isRejected properties.
Those. you can still use these properties, just not in the context of A+.
Take Q or any lightweight replacement, and implement whatever you want with defer.
An example of what you end up with might be @onqu's answer.
The code he provided is exactly what you need, but instead of having a logical unit inside each promise, you have a shared state that you can change anywhere - good luck debugging.
Cancel a promise only inside it. Report state changes only with then. If you need to track the progress of downloading something, do it through events or callbacks:
function doSomethingAsync(timeout, cb) {
var ee = new EventEmitter();
var state = {
progress: 0
};
(function loop() {
if (state.progress === 22) return cb(null, state);
if (state.progress === 'canceled') return cb(new Error('Action canceled'));
if (state.progress * 1000 > timeout) return cb(new Error('Action timed out'));
ee.emit('progress', state);
setTimeout(loop, 1000);
})();
return ee;
}
function a20SecAction(actions = {}) {
var maxActionTime = 20000;
return new Promise((resolve, reject) => {
var actionWithProgress = doSomethingAsync(maxActionTime, (err, result) =>
(err ? reject : resolve)(err || result)
);
actionWithProgress.on('progress', actions.progress);
});
}
a20SecAction({
progress: (state) => console.log('state:', state.progress)
}).then(
(res) => console.log('state: ready'),
(err) => console.log('state:', err)
);
a20SecAction({
progress: (state) => {
console.log('state:', state.progress);
if (state.progress === 7) state.progress = 'canceled';
}
}).then(
(res) => console.log('state: ready'),
(err) => console.log('state:', err)
);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question