Created
August 7, 2015 15:10
-
-
Save dimik/43187cb38ebe64144516 to your computer and use it in GitHub Desktop.
Vow test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Получаю следующее:
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] ] }