寻找如果与异步库,然后其他模式 [英] Looking for if then else pattern with async library

查看:119
本文介绍了寻找如果与异步库,然后其他模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不那么有经验的/优雅的程序员。我希望我的问题是可以理解的。

我已经用java / C ++为我的生活的大部分,所以我的心是面向对象的。后来我才了解到Python和我很喜欢的功能头脑。现在我接近JS。它是具有挑战性的,因为它是基于函数(我喜欢它了很多),功能(带下划线我有很多的Python的 iterutils 方法)和异步(它伤害)。

很多的时候,我有一些后端同步程序流,如:

 如果存在(用户):
    富(用户)
    # 好
其他:
    添加(用户)
    #用户添加

现在我有所谓的回调地狱处理这个问题:

 的存在(用户,功能(userExist){
    如果(userExist){
        美孚(用户,功能(fooResult){
            /* 好 */
        });
    }其他{
        添加(用户,功能(addResult){
            / *用户添加* /
        });
    }
});

有时候控件多个嵌套:检查令牌,如果令牌是用户的有效检查是否存在,如果存在用户检查有效参数,然后在foo的检查没有错误的用户,等等...

这些控件仅仅是同步的,必要的,等等。没有别的可说的。虽然与同步语言如Python我可以处理这(不优雅,但至少)可读code。与使用小型函数返回值,使用javascript我不知道如何重构东西可读的小功能。我写的所有的功能没有任何收益语句,但只是回调(something_weird_defined_in_caller_function),我失去了我自己。

我不认为承诺是好于我的情况,因为管道事件,IIUC较多。于是我找了使用异步库来处理这种情况下,一些模式:
  - 继续在系列执行功能只有在previous 1得手

请任何帮助将AP preciated。


解决方案

  

我不认为承诺是好于我的情况,因为更多的管道事件,IIUC


没有。承诺不重present事件管道流,而是一个的的结果将在稍后(异步)到达。这是的究竟的是什么,他们做的,他们会给你必要的,喜欢看code(而不是一个嵌套回调地狱)。

不可否认,你仍然需要嵌套控制结构的。在code,你presented不会看除了回调进入。然后()通话,承诺为收益编辑。

不过,您也可以 href=\"http://stackoverflow.com/a/24663315/1048572\">使用异常。而不是使用的存在返回一个无极<布尔> ,你可以使用找到函数返回一个无极<使用者> 其中,当用户不存在将被拒绝。它可以用来像

 找到(用户)。然后(功能(/ * *用户/){
    返回美孚(用户)/ *。然后(功能(fooResult){
        ...
    }); * /
})。赶上(UserNotFoundError,功能(E){
    返回附加(用户)/ *。然后(功能(addResult){
        ...
    }); * /
});

其中哪些选择是值得商榷的,拒绝应属例外

I'm not so experienced/elegant programmer. I hope my question is understandable.

I have used java/c++ for most part of my life so my mind is object oriented. Then I learned python and I enjoyed very much functional mind. Now I'm approaching js. It is challenging because it is function based (and I liked it a lot), functional (with underscore I have a lot of pythonic iterutils methods) and asynchronous (it hurts).

A lot of time I have some back-end sync procedural flow like:

if exist(user):
    foo(user)
    # ok
else:
    add(user)
    # user added

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

exists(user, function(userExist) {
    if( userExist ) {
        foo( user, function(fooResult) { 
            /* ok */ 
        });
    } else {
        add( user, function(addResult) { 
            /* user added */ 
        });
    }
});

Sometimes the controls are more nested: check for token, if token is valid check for user exists, if user exists check for valid parameters, then check for no error in foo on user, etc…

Those controls are simply synchronous and imperative-like. Nothing else to say. While with sync language like python I can handle this is (not elegant but at least) readable code with the use of small function that returns values , with javascript I don't know how to refactor things in readable small function. All functions I wrote does not have any return statements but just callback(something_weird_defined_in_caller_function) and I lost myself.

I don't think that promises are good on my case because are more for piping events, IIUC. So I'm looking for some pattern using async library to handle this case: - continue to execute functions in series only if the previous one succeeded.

Please any help will be appreciated.

解决方案

I don't think that promises are good on my case because are more for piping events, IIUC

No. Promises don't represent a stream of events to pipe, but a single result that will arrive later (asynchronously). This is exactly what they were made for, and they will give you imperative-like looking code (instead of a nesting callback hell).

Admittedly, you still need nesting for control structures. The code that you presented won't look much different except for the callbacks going into .then() calls, and promises being returned.

However, you can also use exceptions for control flow. Instead of using exists that returns a Promise<boolean>, you could use a find function that returns a Promise<User> which would get rejected when the user doesn't exist. It could be used like

find(user).then(function(/*user*/) {
    return foo(user) /* .then(function(fooResult) { 
        …
    }); */
}).catch(UserNotFoundError, function(e) {
    return add(user) /* .then(function(addResult) { 
        … 
    }); */
});

Which of those to choose is debatable, rejections should be exceptional.

这篇关于寻找如果与异步库,然后其他模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆