Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 61 additions & 5 deletions flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,28 @@
* Сделано задание на звездочку
* Реализованы методы mapLimit и filterLimit
*/
exports.isStar = true;
exports.isStar = false;

/**
* Последовательное выполнение операций
* @param {Function[]} operations – функции для выполнения
* @param {Function} callback
*/
exports.serial = function (operations, callback) {
console.info(operations, callback);
if (!operations) {
callback(null, null);
}
var index = 0;
var cb = function (error, result) {
index++;
if (error || index === operations.length) {
callback(error, result);
} else {
operations[index](result, cb);
}
};
operations[index](cb);

};

/**
Expand All @@ -21,8 +34,33 @@ exports.serial = function (operations, callback) {
* @param {Function} operation – функция для обработки элементов
* @param {Function} callback
*/

exports.map = function (items, operation, callback) {
console.info(items, operation, callback);
if (!items) {
callback(null, null);
}
var count = items.length;
var errorCallback = false;
var results = [];
var cb = function (index, err, result) {
if (err) {
errorCallback = true;

return callback(err);
}
if (errorCallback) {
return;
}
count--;
results[index] = result;
if (count === 0) {
return callback(null, results);
}
};

items.forEach(function (item, index) {
operation(item, cb.bind(null, index));
});
};

/**
Expand All @@ -32,15 +70,33 @@ exports.map = function (items, operation, callback) {
* @param {Function} callback
*/
exports.filter = function (items, operation, callback) {
console.info(items, operation, callback);
exports.map(items, operation, function (err, result) {
if (err) {
callback(err);
}
callback(null, items.filter(function (item, index) {
return result[index];
}));
});
};

/**
* Асинхронизация функций
* @param {Function} func – функция, которой суждено стать асинхронной
*/

exports.makeAsync = function (func) {
console.info(func);
return function () {
var args = [].slice.call(arguments);
var callback = args.pop();
setTimeout(function () {
try {
callback(null, func.apply(null, args));
} catch (error) {
callback(error);
}
}, 0);
};
};

/**
Expand Down