javascript - Looking for if then else pattern with async library -


I'm not a very experienced / elegant programmer. I hope my question comes out.

I have used Java / C ++ for most of my life, so my mind is object-oriented. Then I learned the dragon and I enjoyed a very functional mind. Now I'm coming to JS because it's challenging because this function is based (and I like it very much), functional (I have a lot of python [code] etterts with underscores [codes] and asynchronous (this pain it happens).

I have a number of back-end sync procedural flows like:

  if present (user): foo (user) is # OK: add (user ) # User Added   

Now I have to handle it with the so called callback hell:

  is present (user, function (userexist) {if (userexist } {FU (user, function (FooResult) {/ * ok * /});} and {add (user, function (addResult) {/ * users Sometimes added controls are nested: check the token, if the token is valid check for the user, if the user exists, So check the valid parameters, then do not check the error on the user in Foo. ?? | | |  

These controls are only synchronous and essential-and there is nothing else to say. I can handle this with sync language (small but less Not less) Readable code that returns values ​​with Javascript, I do not know how to refine things in a readable small function. There is no return statement in all the functions I've written, but only callback (some_error_defined_in_color_function) and I have lost myself.

I do not think that the promise is good in my case because piping is more for the events, IUC. So, I'm looking for some patterns by using the async library to handle this code: - To continue functioning in the series, continue only when the previous one is successful.

Please any help would be appreciated.

I do not think the promises are good in my case because piping has more events, IUCC

No promises do not represent a stream of events for pipes, but an single result will come later (asynchronous). What has been done for this absolutely , and they will give you the mandatory-looking code (instead of a nesting callback hell).

Of course, the code you submitted is .then () except for incoming callbacks in the call, and return ed < / P> However, instead of using existing you can also promise boolean & gt; , you can use a search function which prompts users & gt; / Code> which will be rejected if the user does not exist. It can be used

  find (user). Then (function (/ * user * /) {return foo (user) / * .then (function (fooResult) {a ?? |}); * /}). Hold (UserNotFoundError, function (e) {return add (user) / * .then (function (addResult) {single ?? |}); * /});   

Which of these is the issue of debate to choose from.

Comments

Popular posts from this blog

ios - Adding an SKSpriteNode to SKScene from a child SKSpriteNode -

Matlab transpose a table vector -

c# - Textbox not clickable but editable -