Skip to content

Instantly share code, notes, and snippets.

@dimik
Created August 7, 2015 15:10
Show Gist options
  • Save dimik/43187cb38ebe64144516 to your computer and use it in GitHub Desktop.
Save dimik/43187cb38ebe64144516 to your computer and use it in GitHub Desktop.
Vow test
var vow = require('vow');
var Queue = require('vow-queue');
var queue = new Queue({ weightLimit : 10 });
var tasks = [];
var enqueue = function (task) {
tasks.push(queue.enqueue(task, { priority: 1, weight: 1 }));
};
var getProgress = function (num) {
return Math.round(num * 100 / tasks.length);
};
var createTask = function (i) {
var defer = vow.defer();
setTimeout(function () {
defer.notify('processed: ' + i);
/*
if(i % 2 === 0) {
return defer.reject(new Error('Error ' + i));
}
*/
defer.resolve(i);
}, 100);
return defer.promise();
};
var runTest = function () {
for(var i = 0; i < 10; i++) {
enqueue(createTask(i));
}
queue.start();
vow.allResolved(tasks)
.then(function (promises) {
var results = [], errors = [];
promises.forEach(function (promise, index) {
var value = promise.valueOf();
if(promise.isFulfilled()) {
results.push(value);
}
else {
errors.push(value);
}
});
return {
results: results,
errors: errors
};
})
.progress(function (message) {
console.log('in progress');
var stats = queue.getStats();
return {
message: message,
processed: getProgress(stats.processedTasksCount),
processing: getProgress(stats.processingTasksCount)
};
})
.then(function (res) {
console.log('res: ', res);
})
.fail(function (err) {
console.log('err: ', err);
});
};
runTest();
@dimik
Copy link
Author

dimik commented Aug 7, 2015

Получаю следующее:

res: { results: [],
errors:
[ [TypeError: undefined is not a function],
[TypeError: undefined is not a function],
[TypeError: undefined is not a function],
[TypeError: undefined is not a function],
[TypeError: undefined is not a function],
[TypeError: undefined is not a function],
[TypeError: undefined is not a function],
[TypeError: undefined is not a function],
[TypeError: undefined is not a function],
[TypeError: undefined is not a function] ] }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment