简体   繁体   English

回调地狱和嵌套承诺之间的区别

[英]Difference between callback hell and nested promises

I recently started working with NodeJS and MongoDB(using Monk). 我最近开始使用NodeJS和MongoDB(使用Monk)工作。 This is when I came across the term "callback hell". 这是我遇到“回调地狱”一词的时候。 In my code I was exactly doing the same thing. 在我的代码中,我确实在做同样的事情。 For an example- 例如

DBCall(d1, function(e, docs){
 if(docs.length!=0)
  DBCall(d2, function(e, docs1){
   if(docs1.length!=0)
    DBCall(d3, function(e, docs2){
      //doing something with docs,docs1,docs2
    }) 
  }) 
}) 

This is when I started reading on "promises", and I came across this article - https://strongloop.com/strongblog/promises-in-node-js-with-q-an-alternative-to-callbacks/ 这是我开始阅读“承诺”的时候,我遇到了这篇文章-https://strongloop.com/strongblog/promises-in-node-js-with-q-an-alternative-to-callbacks/

Since I needed both docs and docs1 inside the third callback so I used nested promises. 由于在第三个回调中需要docs和docs1,因此我使用了嵌套的Promise。

DBCall(d1)
.then(function(docs){
  if(docs.length!=0)
   return DBCall(d2)
   .then(function(docs1){
     if(docs1.length!=0)
      return DBCall(d3)
      .then(function(docs2){
        //doing something with docs,docs1,docs2
      })
    })
  })

From the above code snippet I have the following questions(/doubts) : 从上面的代码片段中,我有以下问题(/怀疑):

  1. Apart from making the code more readable, does promises have performance benefits? 除了使代码更具可读性之外,promise是否还具有性能优势?
  2. Nesting promises and callback hell looks similar to me. 嵌套承诺和回调地狱看起来与我相似。 Is there actually any difference? 实际上有什么区别吗?

I am new to this concept of promises. 我对这种承诺的概念是陌生的。 Any help is appreciated. 任何帮助表示赞赏。

Basically the purpose of Promises is to allow for functional composition and error handling, in a way that reads in a synchronous nature. 基本上,Promise的目的是允许功能组合和错误处理,以一种同步方式读取。 Promises allow you to read code in a linear (maybe wrong term) or synchronous fasion. Promise允许您以线性(可能是错误的术语)或同步方式阅读代码。

This is sort of a broad question but take a look through these suggested links. 这是一个广泛的问题,但请查看这些建议的链接。

https://promise-nuggets.github.io/ https://promise-nuggets.github.io/
https://blog.domenic.me/youre-missing-the-point-of-promises/ https://blog.domenic.me/youre-missing-the-point-of-promises/

also this link 还有这个链接

Edit: After reading your updates, what you're essentially asking for is to join the promises (I think) 编辑:阅读您的更新后,您本质上要求的是加入承诺(我认为)

This SO Post gives some good info. SO Post提供了一些很好的信息。 Some of the better libraries have utility functions to help with this. 一些更好的库具有实用程序功能来帮助解决此问题。

For example, if using bluebird take a look at the join function 例如,如果使用bluebird,请看一下join函数

Apart from making the code more readable, does promises have performance benefits? 除了使代码更具可读性之外,promise是否还具有性能优势?

Probably not. 可能不是。

Nesting promises and callback hell looks similar to me. 嵌套承诺和回调地狱看起来与我相似。 Is there actually any difference? 实际上有什么区别吗?

Promises don't automatically prevent callback hell. 承诺不会自动阻止回调地狱。 But because they are more flexible than "simple" callbacks, they can be composed in different ways, which makes it easier to avoid callback hell. 但是,由于它们比“简单”的回调更为灵活,因此它们可以以不同的方式进行组合,这使得避免回调地狱变得更加容易。


Since I needed both docs and docs1 inside the third callback so I used nested promises. 由于在第三个回调中需要docs和docs1,因此我使用了嵌套的Promise。

Sometimes nested promises are unavoidable. 有时,不可避免的是嵌套的承诺。 However, there is no need to nest them if they don't have to be executed sequentially. 但是,如果不必按顺序执行它们,则无需嵌套它们。 You could execute them in parallel: 您可以并行执行它们:

Promise.all([
 DBCall(d1),
 DBCall(d2),
 DBCall(d3)
]).then(function(docs) {
  // docs is an array: [docs, docs1, docs2]
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM