{"id":168929,"date":"2013-02-10T18:15:02","date_gmt":"2013-02-10T14:15:02","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=168929"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=168929","title":{"rendered":"<span class=\"post_title\">\u0412\u0435\u043b\u043e\u0441\u0438\u043f\u0435\u0434: Promise \u0432 Node.js<\/span>"},"content":{"rendered":"<div class=\"content html_format\">   \t\u0414\u043e\u0431\u0440\u044b\u0439 \u0434\u0435\u043d\u044c, \u0425\u0430\u0431\u0440\u0430\u0445\u0430\u0431\u0440.<\/p>\n<h4>\u041f\u0440\u0435\u0434\u0438\u0441\u043b\u043e\u0432\u0438\u0435<\/h4>\n<p>  \u0411\u044b\u043b\u0430 \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u0430\u044f \u0437\u0430\u0434\u0430\u0447\u0430: \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043d\u0430\u0431\u043e\u0440 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u043e\u0432 \u0438\u0437 \u0431\u0430\u0437\u044b, \u043a\u0430\u0436\u0434\u044b\u0439 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u0438 \u043e\u0442\u043f\u0440\u0430\u0432\u0438\u0442\u044c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044e \u0432\u0441\u0435 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u044b, \u043f\u043e\u0440\u044f\u0434\u043e\u043a \u0438\u0445 \u043c\u0435\u043d\u044f\u0442\u044c \u043d\u0435\u043b\u044c\u0437\u044f, \u0434\u043b\u044f \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f. \u0415\u0441\u043b\u0438 \u043d\u0430 \u043a\u0430\u043a\u043e\u043c-\u0442\u043e \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0435 \u0432\u044b\u043b\u0435\u0437\u043b\u0430 \u043e\u0448\u0438\u0431\u043a\u0430 \u2014 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u044b \u043c\u044b \u043d\u0435 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u043c, \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0448\u0438\u0431\u043a\u0443 \u0438 \u0437\u0430\u043a\u0430\u043d\u0447\u0438\u0432\u0430\u0435\u043c \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0443 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u043e\u0432.<br \/>  \u0414\u043b\u044f \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u0437\u0430\u0434\u0430\u0447\u0438 \u0431\u044b\u043b\u0430 \u0432\u044b\u0431\u0440\u0430\u043d\u0430 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430 <a href=\"https:\/\/github.com\/kriskowal\/q\">Q<\/a>, \u0442\u0430\u043a \u043a\u0430\u043a \u0441\u0430\u043c \u043f\u043e\u0445\u043e\u0434 Promise \u043c\u043d\u0435 \u0441\u0438\u043c\u043f\u0430\u0442\u0438\u0447\u0435\u043d. \u041d\u043e \u0432\u043e\u0437\u043d\u0438\u043a\u043b\u0430 \u043e\u0434\u043d\u0430 \u0437\u0430\u0433\u0432\u043e\u0437\u0434\u043a\u0430, \u0432\u0440\u043e\u0434\u0435 \u0431\u044b \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u0440\u043d\u0430\u044f \u0437\u0430\u0434\u0430\u0447\u0430, \u0430 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u0431\u043e\u043b\u044c\u0448\u0435 \u0441\u0435\u043a\u0443\u043d\u0434\u044b, \u0430 \u0442\u043e\u0447\u043d\u0435\u0435 1300 \u043c\u0441, \u0432\u043c\u0435\u0441\u0442\u043e \u043e\u0436\u0438\u0434\u0430\u0435\u043c\u044b\u0445 50-80 \u043c\u0441. \u0414\u0430\u0431\u044b \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u0442\u044c\u0441\u044f, \u043a\u0430\u043a \u0432\u0441\u0435 \u0443\u0441\u0442\u0440\u043e\u0435\u043d\u043e \u0438 \u043f\u0440\u043e\u043d\u0438\u043a\u043d\u0443\u0442\u044c\u0441\u044f \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e\u0441\u0442\u044c\u044e \u0431\u044b\u043b\u043e \u0440\u0435\u0448\u0435\u043d\u043e \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u00ab\u0432\u0435\u043b\u043e\u0441\u0438\u043f\u0435\u0434\u00bb \u043f\u043e\u0434 \u0434\u0430\u043d\u043d\u0443\u044e \u0437\u0430\u0434\u0430\u0447\u0443.<\/p>\n<p>  <a name=\"habracut\"><\/a><\/p>\n<h4>\u041a\u0430\u043a \u0431\u044b\u043b\u043e \u0443\u0441\u0442\u0440\u043e\u0435\u043d\u0430 \u0440\u0430\u0431\u043e\u0442\u0430 \u0441 Q<\/h4>\n<p>  \u0412 \u043f\u0435\u0440\u0432\u0443\u044e \u043e\u0447\u0435\u0440\u0435\u0434\u044c, \u0445\u043e\u0442\u0435\u043b\u043e\u0441\u044c \u0431\u044b \u0440\u0430\u0441\u0441\u043a\u0430\u0437\u0430\u0442\u044c, \u043a\u0430\u043a \u044d\u0442\u043e \u0431\u044b\u043b\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u043e \u0438\u0437\u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e. <\/p>\n<p>  1. \u041f\u0440\u043e\u0446\u0435\u0434\u0443\u0440\u0430, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043f\u0440\u043e\u0445\u043e\u0434\u0438\u043b\u0430 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u043f\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0443 \u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u043b\u0430 \u043d\u0430\u043c promise.  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">forEachSerial<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"javascript\">function forEachSerial(array, func) {     var tickFunction = function (el, index, callback) {         if (func.length == 2)             func(el, callback);         else if (func.length == 3)             func(el, index, callback);     }     var functions = [];     array.forEach(function (el, index) {         functions.push(Q.nfcall(tickFunction, el, index));             });     return Q.all(functions); } <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  2. \u041f\u0440\u043e\u0446\u0435\u0434\u0443\u0440\u0430, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u043b\u0430 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442 \u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u043b\u0430 \u043d\u0430\u043c promise.  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">documentToJSON<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"javascript\">function documentToJSON(el, options) {     var obj = el.toObject(options);     var deferred = Q.defer();     var columns = ['...','...','...'];     forEachSerial(columns,function (el, next) {         el.somyAsyncFunction(options, function (err, result) {             if (err)                 next(err);             else result.somyAsyncFunction2(options, function (err, result) {                 if (!err)                     obj[el] = result;                 next(err);             });         });     }).then(function () {             deferred.resolve(obj);         }, function (err) {             deferred.reject(err);         });     return deferred.promise; } <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  3. \u0418 \u0433\u043e\u043b\u043e\u0432\u043d\u0430\u044f \u043f\u0440\u043e\u0446\u0435\u0434\u0443\u0440\u0430, \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u044e\u0449\u0430\u044f \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044e  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">sendResponse<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"javascript\">exports.get = function (req, res, next) {     dbModel.find(search, {}, { skip: from, limit: limit }).sort({column1: -1}).exec(function (err, result) {         if (err)             next(new errorsHandlers.internalError());         else {             var result = [];             forEachSerial(result,function (el, index, next) {                 documentToJSON(el,options).then(function (obj) {                     result[index] = obj;                     next()                 }, function (err) {                     next(new errorsHandlers.internalError())                 });             }).then(function () {                     res.send({responce: result});                 }, function (err) {                     next(new errorsHandlers.internalError())                 });         }     }); }; <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  \u041a\u0442\u043e-\u0442\u043e \u043c\u043e\u0436\u0435\u0442 \u0437\u0430\u043c\u0435\u0442\u0438\u0442\u044c, \u0447\u0442\u043e \u0432\u0441\u0435\u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u0445 \u00ab\u043e\u0431\u0435\u0449\u0430\u043d\u0438\u0439\u00bb \u043e\u0447\u0435\u043d\u044c \u043c\u043d\u043e\u0433\u043e \u0434\u0430\u0436\u0435 \u0442\u0430\u043c, \u0433\u0434\u0435 \u043e\u043d\u0438 \u043d\u0435 \u0441\u0438\u043b\u044c\u043d\u043e \u043d\u0443\u0436\u043d\u044b, \u0438 \u0447\u0442\u043e \u0442\u0430\u043a\u043e\u0435 \u0431\u043e\u043b\u044c\u0448\u043e\u0435 \u0437\u0430\u0446\u0438\u043a\u043b\u0438\u0432\u0430\u043d\u0438\u0435 \u043f\u0440\u0438\u0432\u0435\u043b\u043e \u043a \u0442\u0430\u043a\u043e\u0439 \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u0438, \u043d\u043e \u043f\u0440\u043e\u0446\u0435\u0434\u0443\u0440\u0430 \u043e\u0442\u0434\u0430\u0435\u0442 \u0432\u0441\u0435\u0433\u043e 20 \u043f\u0440\u043e\u0441\u0442\u044b\u0445 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u043e\u0432, \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u043f\u0440\u0438\u043c\u0438\u0442\u0438\u0432\u043d\u044b \u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u0438\u0445 \u0442\u0430\u043a\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u0443\u0436\u0435 \u0442\u043e\u0447\u043d\u043e \u043d\u0438\u043a\u0443\u0434\u0430 \u043d\u0435 \u0433\u043e\u0434\u0438\u0442\u044c\u0441\u044f. <\/p>\n<h4>\u041f\u0438\u0448\u0435\u043c \u0441\u0432\u043e\u044e \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0443 promise<\/h4>\n<p>  <\/p>\n<h5>\u041a\u0430\u043a \u0432\u043e\u043e\u0431\u0449\u0435 \u00ab\u044d\u0442\u043e\u00bb \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442<\/h5>\n<p>  \u0412 \u0441\u0435\u0442\u0438 \u043f\u043e\u043b\u043d\u043e \u043e\u043f\u0438\u0441\u0430\u043d\u0438\u0439 \u0447\u0442\u043e \u0438 \u043a\u0430\u043a. \u041e\u043f\u0438\u0448\u0443 \u0432\u043a\u0440\u0430\u0442\u0446\u0435. Promise \u2014 \u044d\u0442\u043e \u0441\u0432\u043e\u0435\u0433\u043e \u0440\u043e\u0434\u0430 \u043e\u0431\u0435\u0449\u0430\u043d\u0438\u0435. \u041a\u0430\u043a\u0430\u044f-\u0442\u043e \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u043d\u0430\u043c \u043e\u0431\u0435\u0449\u0430\u0435\u0442 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442, \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0435\u0433\u043e \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e <i>then(success, error)<\/i>, \u0432 \u0441\u0432\u043e\u044e \u043e\u0447\u0435\u0440\u0435\u0434\u044c, \u043f\u0440\u0438 \u0443\u0441\u043f\u0435\u0448\u043d\u043e\u0439 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0435 \u043c\u044b \u043c\u043e\u0436\u0435\u0442 \u043d\u0430\u0437\u043d\u0430\u0447\u0438\u0442\u044c \u043d\u043e\u0432\u044b\u0439 promise \u0438 \u0442\u0430\u043a \u0436\u0435 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0435\u0433\u043e. \u0412 \u0447\u0430\u0441\u0442\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u044d\u0442\u043e \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0442\u0430\u043a:  <\/p>\n<pre><code class=\"javascript\">Promise.then(step1).then(step2).then(step3).then(function () {     \/\/All OK }, function (err) {     \/\/Error in any step }); <\/code><\/pre>\n<p>  \u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u044d\u0442\u0430\u043f\u0430 \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u0442\u0441\u044f \u043a\u0430\u043a \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440 \u0432 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0438 \u0442\u0430\u043a \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e. \u0412 \u0438\u0442\u043e\u0433\u0435 \u043c\u044b \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u043c \u0432\u0441\u0435 \u043e\u0448\u0438\u0431\u043a\u0438 \u0432 \u043e\u0434\u043d\u043e\u043c \u0431\u043b\u043e\u043a\u0435 \u0438 \u0438\u0437\u0431\u0430\u0432\u043b\u044f\u0435\u043c\u0441\u044f \u043e\u0442 \u00ab\u043b\u0430\u043f\u0448\u0438\u00bb.<br \/>  \u0412\u043d\u0443\u0442\u0440\u0438 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u0442\u0430\u043a: \u0441\u043e\u0437\u0434\u0430\u044e\u0442\u0441\u044f \u0441\u043e\u0431\u044b\u0442\u0438\u044f, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0432\u044b\u0437\u044b\u0432\u0430\u044e\u0442\u0441\u044f \u043f\u0440\u0438 \u0443\u0441\u043f\u0435\u0448\u043d\u043e\u043c \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u0438 \u0438\u043b\u0438 \u043f\u0440\u0438 \u043e\u0448\u0438\u0431\u043a\u0435:  <\/p>\n<pre><code class=\"javascript\">var promise = fs.stat(&quot;foo&quot;); promise.addListener(&quot;success&quot;, function (value) {     \/\/ ok }) promise.addListener(&quot;error&quot;, function (error) {     \/\/ error }); <\/code><\/pre>\n<p>  \u0412\u0441\u0435 \u044d\u0442\u043e \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0447\u0438\u0442\u0430\u0442\u044c <a href=\"http:\/\/howtonode.org\/promises\">\u0437\u0434\u0435\u0441\u044c<\/a>.<br \/>  \u0422\u0435\u043e\u0440\u0438\u044f \u0437\u0430\u043a\u043e\u043d\u0447\u0438\u043b\u0430\u0441\u044c, \u043f\u0440\u0438\u0441\u0442\u0443\u043f\u0438\u043c \u043a \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0435.<\/p>\n<h5>\u041d\u0430\u0447\u043d\u0435\u043c \u0441 \u043f\u0440\u043e\u0441\u0442\u043e\u0433\u043e \u2014 Deferred<\/h5>\n<p>  \u0417\u0430\u0434\u0430\u0447\u0435\u0439 \u044d\u0442\u043e\u0433\u043e \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u043d\u0443\u0436\u043d\u044b\u0435 \u043d\u0430\u043c \u0441\u043e\u0431\u044b\u0442\u0438\u044f \u0438 \u0432\u044b\u0434\u0430\u0442\u044c Promise  <\/p>\n<pre><code class=\"javascript\">function deferred() {     this.events = new EventEmitter(); \/\/\u041e\u0431\u044a\u0435\u043a\u0442 \u0441\u043e\u0431\u044b\u0442\u0438\u044f     this.promise = new promise(this); \/\/ \u0412\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u044b\u0439 \u043d\u0430\u043c Promise     this.thenDeferred = []; \/\/\u041f\u043e\u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0438, \u043d\u0443\u0436\u043d\u044b \u0434\u043b\u044f \u0442\u043e\u0433\u043e \u0447\u0442\u043e \u0431\u044b \u043f\u0435\u0440\u0435\u0434\u0430\u0442\u044c \u043e\u0448\u0438\u0431\u043a\u0443 \u0434\u0430\u043b\u044c\u0448\u0435 \u043f\u043e \u0446\u0435\u043f\u043e\u0447\u043a\u0435     var self = this;      \/\/\u0412\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0432 \u0443\u0441\u043f\u0435\u0448\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435     this.resolve = function () {         self.events.emit('completed', arguments);     }     \/\/\u0412\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0432 \u0441\u043b\u0443\u0447\u0430\u0435 \u043e\u0448\u0438\u0431\u043a\u0438     this.reject = function (error) {         self.events.emit('error', error);         \/\/\u041f\u0435\u0440\u0435\u0434\u0430\u0435\u043c \u043e\u0448\u0438\u0431\u043a\u0443 \u0434\u0430\u043b\u044c\u0448\u0435 \u043f\u043e \u0446\u0435\u043f\u043e\u0447\u043a\u0435         self.thenDeferred.forEach(function (el) {             el.reject(error);         });     } } <\/code><\/pre>\n<p>  <\/p>\n<h5>\u041e\u0431\u044a\u0435\u043a\u0442 \u2014 Promise<\/h5>\n<p>  \u0417\u0430\u0434\u0430\u0447\u0430 \u0435\u0433\u043e \u0431\u0443\u0434\u0435\u0442 \u043e\u0442\u0441\u043b\u0435\u0436\u0438\u0432\u0430\u0442\u044c \u0441\u043e\u0431\u044b\u0442\u0438\u044f &quot;<i>completed<\/i>&quot; \u0438 &quot;<i>error<\/i>&quot; \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c \u043d\u0443\u0436\u043d\u044b\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043d\u0430\u0437\u043d\u0430\u0447\u0435\u043d\u044b \u0447\u0435\u0440\u0435\u0437 &quot;<i>then<\/i>&quot; \u0438 \u043e\u0442\u0441\u043b\u0435\u0436\u0438\u0432\u0430\u0442\u044c \u0447\u0442\u043e \u0442\u0430\u043c \u0432\u0435\u0440\u043d\u0443\u043b\u0430 \u044d\u0442\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u044f: \u0435\u0441\u043b\u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0442\u0438\u043b\u0430 \u043d\u0430\u043c \u0435\u0449\u0435 \u043e\u0434\u0438\u043d promise \u0442\u043e \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0430\u0442\u044c\u0441\u044f \u043a \u043d\u0435\u043c\u0443 \u0434\u043b\u044f \u0442\u043e\u0433\u043e \u0447\u0442\u043e \u0431\u044b \u0441\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u043b\u0438 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 <i>then<\/i>, \u0435\u0441\u043b\u0438 \u043f\u0440\u043e\u0441\u0442\u043e \u0434\u0430\u043d\u043d\u044b\u0435, \u0442\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u043f\u043e\u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 <i>then<\/i>, \u0442\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u043c\u044b \u0441\u043c\u043e\u0436\u0435\u043c \u0441\u0442\u0440\u043e\u0438\u0442\u044c \u0446\u0435\u043f\u043e\u0447\u043a\u0438 \u0438\u0437 <i>then<\/i>.  <\/p>\n<pre><code class=\"javascript\">function promise(def) {     this.def = def;     this.completed = false;     this.events = def.events;     var self = this;     var thenDeferred;     self._successListener = null;     self._errorListener = null;      \/\/\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u043c \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f then - \u0431\u0443\u0434\u0435\u0442 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c\u0441\u044f \u043d\u043e\u0432\u044b\u0439 promise     this.then = function (success, error) {         if (success)             self._successListener = success;         if (error)             self._errorListener = error;         thenDeferred = new deferred();         self.def.thenDeferred.push(thenDeferred);         return thenDeferred.promise;     }      \/\/\u041e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u043c \u0443\u0441\u043f\u0435\u0448\u043d\u043e\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u0437\u0430\u0434\u0430\u0447\u0438     this.events.on('completed', function (result) {         \/\/ \u043e\u0431\u044a\u0435\u043a\u0442\u044b, \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u044b, \u043c\u0430\u0441\u0441\u0438\u0432\u044b \u043f\u0440\u0438\u0432\u043e\u0434\u0438\u043c \u043a \u0432\u0438\u0434\u0443 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0434\u043b\u044f \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0438 \u0438\u0445 \u0432 \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u043c \u043a\u0430\u043a \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u044b \u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u044e         var args = inputOfFunctionToArray(result);         \/\/\u0415\u0441\u043b\u0438 \u0432\u0434\u0440\u0443\u0433 \u0437\u0430\u0434\u0430\u0447\u0430 \u0431\u044b\u043b\u0430 \u0443\u0436\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0430, \u0442\u043e \u0434\u0430\u043b\u044c\u0448\u0435 \u043d\u0435 \u043f\u0440\u043e\u0445\u043e\u0434\u0438\u043c         if (self.completed) return;         self.completed = true;          if (self._successListener) {             var result;             try {                 result = self._successListener.apply(self, args);             } catch (e) {                 self.def.reject(e);                 result;             }             \/\/\u0415\u0441\u043b\u0438 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u0438 Promise \u0438 \u0435\u0441\u0442\u044c \u043f\u043e\u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 then, \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0430\u0435\u043c\u0441\u044f \u043a \u043d\u0435\u043c\u0443             var promise;             if (isPromise(result))                 promise = result;             else if (result instanceof deferred)                 promise = result.promise;             if (promise && thenDeferred) {                 promise.then(function () {                     var args = arguments;                     process.nextTick(function () {                         thenDeferred.resolve.apply(self, args);                     });                 }, function (error) {                     process.nextTick(function () {                         thenDeferred.reject(error);                     });                 });             } else if (thenDeferred)                            process.nextTick(function () {                                    \/\/\u0414\u043b\u044f \u0441\u043a\u0430\u043b\u044f\u0440\u043d\u044b\u0445 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432 \u043f\u0440\u043e\u0441\u0442\u043e \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 then                                   thenDeferred.resolve.apply(self, [result]);                           });         } else if (thenDeferred)             process.nextTick(function () {                 thenDeferred.resolve.apply(self, []);             });     });      \/\/\u041e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u043c \u043e\u0448\u0438\u0431\u043a\u0438     this.events.on('error', function (error) {         if (self.completed) return;         self.completed = true;         if (self._errorListener)             process.nextTick(function () {                 self._errorListener.apply(self, [error]);             });     }); } <\/code><\/pre>\n<p>  \u0418\u0442\u0430\u043a, \u0431\u0430\u0437\u043e\u0432\u0430\u044f \u043c\u043e\u0434\u0435\u043b\u044c \u0433\u043e\u0442\u043e\u0432\u0430. \u041e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u043e\u0431\u0432\u044f\u0437\u043a\u0443 \u0434\u043b\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u0439 \u0441 <i>callback<\/i><\/p>\n<h5>PromiseFn<\/h5>\n<p>  \u0415\u0433\u043e \u0437\u0430\u0434\u0430\u0447\u0430 \u2014 \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u043e\u0431\u0435\u0440\u0442\u043a\u0443 \u0434\u043b\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0441 <i>callback<\/i> \u0441 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c\u044e \u0443\u043a\u0430\u0437\u0430\u043d\u0438\u044f <i>this<\/i> \u0438 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u0432 \u0437\u0430\u043f\u0443\u0441\u043a\u0430  <\/p>\n<pre><code class=\"javascript\">var promisefn = function (bind, fn) {     var def = new deferred();     \/\/bind \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043d\u0435 \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u043c \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u043c     if (typeof bind === 'function' && !fn) {         fn = bind;         bind = def;     }     \/\/\u041d\u0430\u0437\u043d\u0430\u0447\u0430\u0435\u043c \u043d\u0430\u0448 callback \u0434\u043b\u044f \u0434\u0430\u043d\u043d\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438     var callback = function (err) {         if (err)             def.reject(err);         else {             var args = [];             for (var key in arguments)                 args.push(arguments[key]);             args.splice(0, 1);             def.resolve.apply(bind, args);         }     };     var result = function () {          var args = [];         for (var key in arguments)             args.push(arguments[key]);         args.push(callback);         process.nextTick(function () {             fn.apply(bind, args);         });         return def.promise;     }     return result; } <\/code><\/pre>\n<h5>\u0418 \u043d\u0430\u043f\u043e\u0441\u043b\u0435\u0434\u043e\u043a ALL \u2014 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0439 \u0441 callback<\/h5>\n<p>  \u0422\u0443\u0442 \u0432\u0441\u0435 \u043f\u0440\u043e\u0441\u0442\u043e: \u043d\u0430\u043c \u043f\u0435\u0440\u0435\u0434\u0430\u044e\u0442 \u043c\u0430\u0441\u0441\u0438\u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u0439, \u043c\u044b \u0438\u0445 \u043e\u0431\u0432\u044f\u0437\u044b\u0432\u0430\u0435\u043c \u0447\u0435\u0440\u0435\u0437 promisefn \u0438, \u043a\u043e\u0433\u0434\u0430 \u043e\u043d\u0438 \u0432\u0441\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u0441\u044f \u2014 \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u043c <i>resolve<\/i>  <\/p>\n<pre><code class=\"javascript\">var all = function (functions) {     var def = new deferred();     process.nextTick(function () {         var index = -1;         var result = [];         var next = function (err, arguments) {             if (err) {                 def.reject(err);                 return;             }             if (arguments) result.push(inputOfFunctionToArray(arguments));             index++;             if (index &gt;= functions.length) {                 def.resolve(result);             } else process.nextTick(function () {                 promisefn(functions[index])().then(function () {                     var args = arguments;                     process.nextTick(function () {                         next(err, args);                     });                 }, function (err) {                     process.nextTick(function () {                         next(err);                     });                 });             });         }         process.nextTick(next);      });     return def.promise; } <\/code><\/pre>\n<h4>\u0412 \u0437\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/h4>\n<p>  \u041f\u043e\u0441\u043b\u0435 \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0441\u0442\u0430\u0440\u044b\u0439 \u043f\u043e\u0434\u0445\u043e\u0434 (\u0447\u0435\u0440\u0435\u0437 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0443 Q) \u0431\u044b\u043b \u043f\u0435\u0440\u0435\u043f\u0438\u0441\u0430\u043d <s>\u0437\u0430\u043c\u0435\u043d\u0435\u043d\u043e \u043f\u0430\u0440\u0443 \u043e\u0431\u044a\u044f\u0432\u043b\u0435\u043d\u0438\u0439<\/s> \u0438 \u0437\u0430\u043f\u0443\u0449\u0435\u043d \u0432 \u0442\u0435\u0445 \u0436\u0435 \u0443\u0441\u043b\u043e\u0432\u0438\u044f\u0445. \u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u043f\u043e\u043b\u043e\u0436\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u2014 50-100 \u043c\u0441 (\u0432\u043c\u0435\u0441\u0442\u043e \u043f\u0440\u0435\u0436\u043d\u0438\u0445 1300 \u043c\u0441). <br \/>  \u0412\u0441\u0435 \u0438\u0441\u0445\u043e\u0434\u043d\u0438\u043a\u0438 \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u044b \u043d\u0430 <a href=\"https:\/\/github.com\/a696385\/promise\">Githab<\/a>, \u0442\u0430\u043c \u0436\u0435 \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u0439\u0442\u0438 \u0438 \u043f\u0440\u0438\u043c\u0435\u0440\u044b. \u0418\u0437\u043e\u0431\u0440\u0435\u0442\u0435\u043d\u0438\u0435 \u00ab\u0432\u0435\u043b\u043e\u0441\u0438\u043f\u0435\u0434\u043e\u0432\u00bb \u043f\u043e\u043b\u0435\u0437\u043d\u043e \u0445\u043e\u0442\u044f \u0431\u044b \u0442\u0435\u043c, \u0447\u0442\u043e \u044d\u0442\u043e \u0443\u043b\u0443\u0447\u0448\u0430\u0435\u0442 \u043f\u043e\u043d\u0438\u043c\u0430\u043d\u0438\u0435. <br \/>  \u0421\u043f\u0430\u0441\u0438\u0431\u043e \u0437\u0430 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435!    \t \t\t   \t<\/p>\n<div class=\"clear\"><\/div>\n<\/p><\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"http:\/\/habrahabr.ru\/post\/168929\/\"> http:\/\/habrahabr.ru\/post\/168929\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"content html_format\">   \t\u0414\u043e\u0431\u0440\u044b\u0439 \u0434\u0435\u043d\u044c, \u0425\u0430\u0431\u0440\u0430\u0445\u0430\u0431\u0440.<\/p>\n<h4>\u041f\u0440\u0435\u0434\u0438\u0441\u043b\u043e\u0432\u0438\u0435<\/h4>\n<p>  \u0411\u044b\u043b\u0430 \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u0430\u044f \u0437\u0430\u0434\u0430\u0447\u0430: \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043d\u0430\u0431\u043e\u0440 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u043e\u0432 \u0438\u0437 \u0431\u0430\u0437\u044b, \u043a\u0430\u0436\u0434\u044b\u0439 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u0438 \u043e\u0442\u043f\u0440\u0430\u0432\u0438\u0442\u044c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044e \u0432\u0441\u0435 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u044b, \u043f\u043e\u0440\u044f\u0434\u043e\u043a \u0438\u0445 \u043c\u0435\u043d\u044f\u0442\u044c \u043d\u0435\u043b\u044c\u0437\u044f, \u0434\u043b\u044f \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f. \u0415\u0441\u043b\u0438 \u043d\u0430 \u043a\u0430\u043a\u043e\u043c-\u0442\u043e \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0435 \u0432\u044b\u043b\u0435\u0437\u043b\u0430 \u043e\u0448\u0438\u0431\u043a\u0430 \u2014 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u044b \u043c\u044b \u043d\u0435 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u043c, \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0448\u0438\u0431\u043a\u0443 \u0438 \u0437\u0430\u043a\u0430\u043d\u0447\u0438\u0432\u0430\u0435\u043c \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0443 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u043e\u0432.<br \/>  \u0414\u043b\u044f \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u0437\u0430\u0434\u0430\u0447\u0438 \u0431\u044b\u043b\u0430 \u0432\u044b\u0431\u0440\u0430\u043d\u0430 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430 <a href=\"https:\/\/github.com\/kriskowal\/q\">Q<\/a>, \u0442\u0430\u043a \u043a\u0430\u043a \u0441\u0430\u043c \u043f\u043e\u0445\u043e\u0434 Promise \u043c\u043d\u0435 \u0441\u0438\u043c\u043f\u0430\u0442\u0438\u0447\u0435\u043d. \u041d\u043e \u0432\u043e\u0437\u043d\u0438\u043a\u043b\u0430 \u043e\u0434\u043d\u0430 \u0437\u0430\u0433\u0432\u043e\u0437\u0434\u043a\u0430, \u0432\u0440\u043e\u0434\u0435 \u0431\u044b \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u0440\u043d\u0430\u044f \u0437\u0430\u0434\u0430\u0447\u0430, \u0430 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u0431\u043e\u043b\u044c\u0448\u0435 \u0441\u0435\u043a\u0443\u043d\u0434\u044b, \u0430 \u0442\u043e\u0447\u043d\u0435\u0435 1300 \u043c\u0441, \u0432\u043c\u0435\u0441\u0442\u043e \u043e\u0436\u0438\u0434\u0430\u0435\u043c\u044b\u0445 50-80 \u043c\u0441. \u0414\u0430\u0431\u044b \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u0442\u044c\u0441\u044f, \u043a\u0430\u043a \u0432\u0441\u0435 \u0443\u0441\u0442\u0440\u043e\u0435\u043d\u043e \u0438 \u043f\u0440\u043e\u043d\u0438\u043a\u043d\u0443\u0442\u044c\u0441\u044f \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e\u0441\u0442\u044c\u044e \u0431\u044b\u043b\u043e \u0440\u0435\u0448\u0435\u043d\u043e \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u00ab\u0432\u0435\u043b\u043e\u0441\u0438\u043f\u0435\u0434\u00bb \u043f\u043e\u0434 \u0434\u0430\u043d\u043d\u0443\u044e \u0437\u0430\u0434\u0430\u0447\u0443.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-168929","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/168929","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=168929"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/168929\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=168929"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=168929"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=168929"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}