Node.js, the pyramid of doom (even with async), can you write it better?

前端 未结 5 959
隐瞒了意图╮
隐瞒了意图╮ 2021-01-15 03:15

I consider myself a very experienced node.js developer.

Yet I still wonder if there is a better way to write the following code so I don\'t get the pyramid of doom..

5条回答
  •  醉梦人生
    2021-01-15 03:28

    The simplest way to combat the async pyramid of hell is to segregate your async callbacks into smaller functions that you can place outside your main loop. Chances are you can at least break some of your callbacks into more maintainable functions that can be used elsewhere in your code base, but the question you're asking is a bit vague and can be solved in a large number of ways.

    Also, you should consider what Stuart mentioned in his answer and try to combine some of your queries together. I'm more concerned that you have 20+ nested calls which would indicate something seriously erroneous in your callback structure so I'd look at your code first before anything else.

提交回复
热议问题