什么是“回叫地狱”以及RX如何和为什么解决它? [英] What is "callback hell" and how and why RX solves it?

查看:179
本文介绍了什么是“回叫地狱”以及RX如何和为什么解决它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以给一个清楚的定义,一个简单的例子,解释什么是一个回调地狱的人谁不知道javascript和node.js?



什么时候(在什么样的设置)回调地狱问题发生?



为什么会发生?



回调地狱总是与异步计算有关吗?



还可以在单​​线程应用程序中发生回调地狱吗?



我问这个问题,因为我在Coursera和Erik Meijer采取了活动课程,他在他的一个讲座中说RX解决了回调地狱的问题。我问Coursera论坛上是什么回调地狱,但我没有明确的答案(我仍然不知道什么回调地狱的意思)。



在一个简单的例子中解释了回调地狱之后,你能不能也展示RX如何解决这个简单例子中的回调地狱问题?

解决方案

1)对于不知道javascript和node.js的人,什么是回调地狱?



这个另一个问题有一些Javascript回调地狱的例子:如何避免Node.js中异步函数的长嵌套



JavaScript中的问题是, 一个计算和剩下的执行后面的(异步)是把剩下的放在一个回调中。



例如,运行如下代码:

  x = getData(); 
y = getMoreData(x);
z = getMoreData(y);
...

如果现在我想让getData函数异步,我有机会运行一些其他代码,而我等待他们返回他们的值?在Javascript中,唯一的方法是使用继续传递风格

  getData(function(x){
getMoreData(x,function(y){
getMoreData(y,function(z){
...
});
});
} );

我不认为我需要说服任何人,这个版本比上一个更丑陋。 : - )



2)何时(在什么样的设置中)发生回调地狱问题?



当你的代码中有很多回调函数时!它更难与他们合作,在你的代码中有更多的,当你需要做循环,try-catch块和这样的东西,它变得特别糟糕。



例如,据我所知,在JavaScript中,执行一系列异步函数的唯一方法是使用递归函数,其中一个在前一个返回之后运行。

  //我们要写
for(var i = 0; i< ; 10; i ++){
doSomething(i);
}
blah();相反,我们可能需要最终写成:

  function loop(i,onDone){
if(i> = 10){
onDone()
} else {
doSomething(i,function(){
loop(i + 1);
});
}
}
loop(0,function(){
blah();
});

// ugh!

JavaScript标签询问如何执行此操作的问题数量应该是如何令人困惑是:)



3)为什么会出现这种情况?



一个计算使它在异步调用返回之后运行是将延迟代码放在一个回调函数中。



4)也可以在单线程应用程序中发生回调地狱 ?



异步编程与并发有关,而单线程与并行性有关。这两个概念实际上是不一样的。



你仍然可以在单个线程上下文中有并发代码。事实上,JavaScript,回调地狱的女王,是单线程的。



并发与并行性 - 有什么区别?



5)你也可以展示如何RX解决了这个简单例子中的回调地狱问题。



我不知道任何关于RX的特别,但通常这个问题通过添加原生支持在编程语言中的异步计算。这可以很多,可以有不同的名称(异步,生成器,协程,callcc,...)。对于一个cr脚的具体例子,在Python中,我们可以使用生成器来代码循环示例与以下行:

  def myLoop():
for i in range(10):
doSomething(i)
yield

myGen = myLoop()

这不是完整的代码,但是想法是yield暂停我们的for循环,直到有人调用myGen.next重要的是,我们仍然可以使用for循环编写代码,而不需要像在循环循环函数中那样转换逻辑内部。


Can someone please give a clear definition together with a simple example that explains what is a "callback hell" for someone who does not know javascript and node.js ?

When (in what kind of settings) does the "callback hell problem" occur?

Why does it occur ?

Is "callback hell" always related to asynchronous computations?

Or can "callback hell" occur also in a single threaded application?

I am asking this because I took the Reactive Course at Coursera and Erik Meijer said in one of his lectures that RX solves the problem of "callback hell". I asked what is a "callback hell" on the Coursera forum but I got no clear answer (and I still don't know what "callback hell" means).

After explaining "callback hell" on a simple example, could you please also show how RX solves the "callback hell problem" on that simple example?

解决方案

1) What is a "callback hell" for someone who does not know javascript and node.js ?

This other question has some examples of Javascript callback hell: How to avoid long nesting of asynchronous functions in Node.js

The problem in Javascript is that the only way to "freeze" a computation and have the "rest of it" execute latter (asynchronously) is to put "the rest of it" inside a callback.

For example, say I want to run code that looks like this:

x = getData();
y = getMoreData(x);
z = getMoreData(y);
...

What happens if now I want to make the getData functions asynchronous, meaning that I get a chance to run some other code while I am waiting for them to return their values? In Javascript, the only way would be to recode everything that touches an async computation using continuation passing style:

getData(function(x){
    getMoreData(x, function(y){
        getMoreData(y, function(z){ 
            ...
        });
    });
});

I don't think I need to convince anyone that this version is uglier than the previous one. :-)

2) When (in what kind of settings) does the "callback hell problem" occur?

When you have lots of callback functions in your code! It gets harder to work with them the more of them you have in your code and it gets particularly bad when you need to do loops, try-catch blocks and things like that.

For example, as far as I know, in JavaScript the only way to execute a series of asynchronous functions where one is run after the previous returns is using a recursive function. You cant use a for loop.

//we would like to write
for(var i=0; i<10; i++){
    doSomething(i);
}
blah();

Instead, we might need to end up writing:

function loop(i, onDone){
    if(i >= 10){
        onDone()
    }else{
        doSomething(i, function(){
            loop(i+1);
        });
     }
}
loop(0, function(){
    blah();
});

//ugh!

The number of questions the JavaScript tag gets asking how to do this should be a testament to how confusing it is :)

3) Why does it occur ?

It occurs because in JavaScript the only way to delay a computation so that it runs after the asynchronous call returns is to put the delayed code inside a callback function. You cannot delay code that was written in traditional synchronous style so you end up with nested callbacks everywhere.

4) Or can "callback hell" occur also in a single threaded application?

Asynchronous programming has to do with concurrency while a single-thread has to do with parallelism. The two concepts are actually not the same thing.

You can still have concurrent code in a single threaded context. In fact, JavaScript, the queen of callback hell, is single threaded.

Concurrency vs Parallelism - What is the difference?

5) could you please also show how RX solves the "callback hell problem" on that simple example.

I don't know anything about RX in particular, but usually this problem gets solved by adding native support for asynchronous computation in the programming language. This can very a lot and can come in different names (async, generators, coroutines, callcc, ...). For a crappy concrete example, in Python we can use generators to to code that loop example with something along the lines of:

def myLoop():
    for i in range(10):
        doSomething(i)
        yield

myGen = myLoop()

This is not the full code but the idea is that the "yield" pauses our for loop until someone calls myGen.next(). The important thing is that we could still write the code using a for loop, without needing to turn out logic "inside out" like we had to do in that recursive loop function.

这篇关于什么是“回叫地狱”以及RX如何和为什么解决它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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