From f16750535c00d68d81a3fb4ae83f097cf974b69d Mon Sep 17 00:00:00 2001 From: PavelTrofimov Date: Thu, 1 Dec 2016 00:14:09 +0600 Subject: [PATCH 1/2] test --- flow.js | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 55 insertions(+), 5 deletions(-) diff --git a/flow.js b/flow.js index d46e521..9318531 100644 --- a/flow.js +++ b/flow.js @@ -4,7 +4,7 @@ * Сделано задание на звездочку * Реализованы методы mapLimit и filterLimit */ -exports.isStar = true; +exports.isStar = false; /** * Последовательное выполнение операций @@ -12,7 +12,17 @@ exports.isStar = true; * @param {Function} callback */ exports.serial = function (operations, callback) { - console.info(operations, callback); + 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); + }; /** @@ -21,8 +31,30 @@ exports.serial = function (operations, callback) { * @param {Function} operation – функция для обработки элементов * @param {Function} callback */ + exports.map = function (items, operation, callback) { - console.info(items, operation, callback); + 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)); + }); }; /** @@ -32,15 +64,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); + }; }; /** From 00a0a51a47fba55d2f0ce7553543a730b8ab4179 Mon Sep 17 00:00:00 2001 From: PavelTrofimov Date: Thu, 1 Dec 2016 00:27:57 +0600 Subject: [PATCH 2/2] =?UTF-8?q?=D0=BF=D1=80=D0=BE=D0=B2=D0=B5=D1=80=D0=BA?= =?UTF-8?q?=D0=B0=20=D0=B2=D1=85=D0=BE=D0=B4=D0=BD=D1=8B=D1=85=20=D0=B7?= =?UTF-8?q?=D0=BD=D0=B0=D1=87=D0=B5=D0=BD=D0=B8=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- flow.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/flow.js b/flow.js index 9318531..e7a2d6a 100644 --- a/flow.js +++ b/flow.js @@ -12,6 +12,9 @@ exports.isStar = false; * @param {Function} callback */ exports.serial = function (operations, callback) { + if (!operations) { + callback(null, null); + } var index = 0; var cb = function (error, result) { index++; @@ -33,6 +36,9 @@ exports.serial = function (operations, callback) { */ exports.map = function (items, operation, callback) { + if (!items) { + callback(null, null); + } var count = items.length; var errorCallback = false; var results = [];