Skip to content

Instantly share code, notes, and snippets.

@A
Last active August 29, 2015 14:00
Show Gist options
  • Save A/11168586 to your computer and use it in GitHub Desktop.
Save A/11168586 to your computer and use it in GitHub Desktop.
Примеры использования модуля next-done
// Ожидание завершения сборки всех модулей
module.exports = function (done) {
var modules = config.get('clinch');
var keys = Object.keys(modules);
var next = new Countdown(keys.length, done);
for (var i = keys.length - 1; i >= 0; i--) {
var key = keys[i];
new Compilation(modules[key], next);
}
};
// Отправка ответа после получения результатов двух запросов к БД.
// Здесь явно указано колличество колбеков, но от него можно избавиться,
// перечислив запросы в виде массива. Учитывая размер `next-done`, это, наверное, вполне ок.
var _getUsersInDialogs = function (user, cb) {
var authors, users;
var next = new Countdown(2, function () {
users = users
.concat(authors)
.filter(function (value, index, self) {
return self.indexOf(value) === index;
});
return cb(null, users);
});
Message
.distinct('author', {user: user}, function (err, _authors) {
authors = _authors;
next();
});
Message
.distinct('user', {author: user}, function (err, _users) {
users = _users;
next();
});
};
// Вызов колбека после удаления всех перечисленных моделей
it('Удаление тестовых данных', function (done) {
var Collections = [
mongoose.model('User'),
mongoose.model('Room'),
mongoose.model('Session'),
mongoose.model('Message')
];
var next = new Countdown(Collections.length, done);
Collections.forEach(function (Collection) {
Collection.remove(function (err) {
next(err);
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment