In the previous "JavaScript promises mode-pretty cool callback Hell Terminator" we introduced the promise pattern of JavaScript, and then we used JavaScript promise in our code.
JavaScript Promise Library Q
Tried to use Q before, but did not succeed. In other words, there is no need to use Q at that time, so I did not delve. Now holding the attitude of learning, try again, the effect is good.
A tool for making and composing asynchronous promises in JavaScript
Q is a JavaScript tool that provides the creation and authoring of asynchronous promise. Q provides some auxiliary functions that can be used to adapt node and other environments to promise.
Reprint reservation: "NodeJS multiple callback solution use Q Promises"
JavaScript Promise Library Q sample
The official website gave a simple explanation of the conversion
Step1 (function (value1) { Step2 (value1, function (value2) { step3 (value2, function (value3) { step4 (value3 , function (value4) { //do something with Value4 }); });
Convert him to
Q.fcall (PROMISEDSTEP1). Then (PROMISEDSTEP2) and then (PROMISEDSTEP3). Then (PROMISEDSTEP4). Then (function (value4) { Do something with value4}). catch (function (error) { //Handle any error from all above steps}). Done ();
But we didn't understand what we were doing.
JavaScript Promise Library Q Combat
The native code is like this, using the Async library
Async.Parallel ([ function () { ' use strict '; Pr.get (domain, next); }, function () { ' use strict '; Gs.get (name, next); }, function () { ' use strict '; Csdn.get (name, next); }, function () { ' use strict '; Zhihu.get (name, next); }, function () { ' use strict '; Alexa.get (domain, next); }]);
But the total feeling was a little messy, but at least left the so-called callback pits.
The process is basically when we need to constantly add something to our result.
The code is then changed to promise form, and then it becomes like this.
Github.promise_get (response, name) . Then (function (Result) { return Pr.promise_get (result, domain); }) . Then (function (Result) { return Csdn.promise_get (result, name); }) . Then (function (Result) { return Zhihu.promise_get (result, name); }) . Then (function (Result) { return Alexa.promise_get (result, domain); }) . Then (function (Result) { callback (result); });
But this looks a little bad because we solidify the process in the code and try to refactor it in other ways.
After the first step of refactoring, it becomes like this.
var info = Information.prototype;info.pagerank_get = function (result) {' Use strict '; return Pagerank.promise_get (result, Information.prototype.domain);}; Info.alexa_get = function (result) {' Use strict '; return Alexa.promise_get (result, Information.prototype.domain);}; info.csdn_get= function (Result) {' Use strict '; return Csdn.promise_get (result, info.name);}; info.github_get= function (Result) {' Use strict '; return Github.promise_get (result, info.name);}; Info.zhihu_get = function (result) {' Use strict '; return Zhihu.promise_get (result, info.name);}; Info.initval = function (result) {' Use strict '; result = []; Return Result;};i Nformation.prototype.get = function (callback) {' Use strict '; Q.fcall (Info.initval). Then (Info.github_get) and then (Info.csdn_get). Then (Info.zhihu_get ()). N (info.pagerank_get). Then (Info.alexa_get). Then (function (Result) {callback (result); });};
Put forward each method first, then we can choose the library that we need to use. It looks more neat than it is, but we still need the next step in order to continue.
NodeJS multiple callback solution use Q Promises