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
89 changes: 84 additions & 5 deletions flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,33 @@
* Сделано задание на звездочку
* Реализованы методы 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.length === 0 || !operations) {
callback(null, null);

return;
}

var currentIndex = 0;
function innerCallback(error, data) {
if (error || currentIndex === operations.length - 1) {
callback(error, data);

return;
}

currentIndex++;
operations[currentIndex](data, innerCallback);
}

operations[currentIndex](innerCallback);
};

/**
Expand All @@ -22,7 +40,31 @@ exports.serial = function (operations, callback) {
* @param {Function} callback
*/
exports.map = function (items, operation, callback) {
console.info(items, operation, callback);
if (items.length === 0 || !items) {
callback(null, []);

return;
}
var result = [];
var resultCount = 0;
var errorExist = false;

items.forEach(function (item, i) {
operation(item, innerCallback.bind(null, i));
});

function innerCallback(index, err, data) {
resultCount++;
if (err && !errorExist) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

пока не очень понимаю, зачем нужен errorExist

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Без этого флага Хрюндель не засчитывает
image

callback(err);
errorExist = true;
} else {
result[index] = data;
if (resultCount === items.length) {
callback(null, result);
}
}
}
};

/**
Expand All @@ -32,15 +74,52 @@ exports.map = function (items, operation, callback) {
* @param {Function} callback
*/
exports.filter = function (items, operation, callback) {
console.info(items, operation, callback);
if (items.length === 0 || !items) {
callback(null, []);

return;
}

exports.map(items, operation, innerCallback);

function innerCallback(err, data) {
if (err) {
callback(err);

return;
}

var result = [];

data.forEach(function (item, i) {
if (item === true) {
result.push(items[i]);
}
});

callback(null, result);
}
};

/**
* Асинхронизация функций
* @param {Function} func – функция, которой суждено стать асинхронной
* @returns {Function} func
*/
exports.makeAsync = function (func) {
console.info(func);
return function () {
setTimeout(function (args) {
if (args.length === 0) {
throw new TypeError('Missing callback');
}
var callback = args[args.length - 1];

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

А если не передадут никаких аргументов?

try {
callback(null, func.apply(null, args.slice(0, args.length - 1)));
} catch (err) {
callback(err);
}
}, 0, [].slice.call(arguments));
};
};

/**
Expand Down