I wanted to use Q
Promise
Progress functionality, I have this code and i want to catch progress and when progress is 100, then resolve the Promise
:
var q = require("q");
var a = function(){
return q.Promise(function(resolve, reject, notify){
var percentage = 0;
var interval = setInterval(function() {
percentage += 20;
notify(percentage);
if (percentage === 100) {
resolve("a");
clearInterval(interval);
}
}, 500);
});
};
var master = a();
master.then(function(res) {
console.log(res);
})
.then(function(progress){
console.log(progress);
});
But i get this error:
Error: Estimate values should be a number of miliseconds in the future
Why?
I don't get this error, if i try to run your script (node 4.2.1), but you never listen to the progress of the promise. You need to register the progressHandler as third parameter to the .then
function:
var q = require("q");
var a = function(){
return q.Promise(function(resolve, reject, notify){
var percentage = 0;
var interval = setInterval(function() {
percentage += 20;
notify(percentage);
if (percentage === 100) {
resolve("a");
clearInterval(interval);
}
}, 500);
});
};
function errorHandler(err) {
console.log('Error Handler:', err);
}
var master = a();
master.then(function(res) {
console.log(res);
},
errorHandler,
function(progress){
console.log(progress);
});
Output:
20
40
60
80
100
a
You must register the progress callback as third parameter to the .then
-function or you can use the special .progress()
shorthand, see https://github.com/kriskowal/q#progress-notification
Here is the call chain with the progress
shorthand:
var master = a();
master.progress(function(progress{
console.log(progress)})
.then(function(res) {
console.log(res);
});
In your code, console.log(progress) prints undefined
, because the function is listen to the result of the previous .then
-statement, which returns nothing.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.