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

前端 未结 5 957
隐瞒了意图╮
隐瞒了意图╮ 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:33

    The problem to this sort of thing is promises. If you haven't heard of them, I suggest reading up on kriskowal's q.

    Now, I don't know if the db.query returns a promise or not. If it doesn't you might be able to find a db-wrapper that does or a different db library. If that is not an option, you may "promisify" the db-library you're using. See Howto use promises with Node, and especially the section "Wrapping a function that takes a Node-style callback".

    Best of luck! :)

提交回复
热议问题