javascript - A bit confused with q and promises in nodejs -


I currently have some JS files in the nodes that are loaded as modules and increasing the app object ( Using Express).

Then their signature looks like this:

  module.exports = function (app, callback) {// ... callback (); }  

So I currently have my code of about 5 will look like this:

  is required (". / Setup / a") (App, function) (". / Setup / b") is required (ap, function () {requires (". / Setup / c") (app, function) {requirement (". / Setup / d "(App, function () {requires (". / Setup / e ") (app, function () {startapp ();}}}}}})}}});  

Now it looks crappy in the form of "Pyramid of Doom", although I am not completely convinced that I need to change this method to use Q because I was assuming that I Q.fcall (... a) then (... b) .etc.done () . Although I am unsure of how I can pass this app and if I have been promised for it then the callback needs to be returned.

Ideally, I do not want to start cue completely through my code, in those places where I want to remove cases of pyramid use, so in the above example, How do I use the Q with the promise of passing the app and then start the app at the end?

Assuming your modules that you can do something like this, do not do this already:

  Module // Expert (app) {// Some things come with some apps, some new apps come back (Profitization (Dismiss, Disclaimer) One CB Ultimate}}}}} Promise .all ([". / Setup / A", ". / Setup / b", "./setup / c"] .Map (required.top (empty, app)). Then (startApp);  

You should use the promise at the lowest level, which means that you Only you can return the promise you used in this process:

  module.exports = function (app) {some refund (app). Then (function () {anything Elseenweth App (App);});}  

So the Promise Producer is not required. Note that this answer uses original promise but it will also work on those libraries which Let's use syntax as Bluebird. Change to Q New Promise From new Q.Promise and Promise.all to Q.all .

Alternatively, you can change the requirement of each (x) to Q.fcall (required, x) and Q .all can be used on directly, but it is both slow (although the Q is slow) and prone to more error prone than promising module. The lowest level API is best to make possible.


Comments

Popular posts from this blog

java - org.apache.http.ProtocolException: Target host is not specified -

java - Gradle dependencies: compile project by relative path -

ruby on rails - Object doesn't support #inspect when used with .include -