What is the suggested callback style for Node.js libraries?

后端 未结 1 330
花落未央
花落未央 2021-01-18 11:22

With focus on how errors are handled:

  • There\'s the style that fs promotes: One callback where the first argument is an error (if any), and the

1条回答
  •  被撕碎了的回忆
    2021-01-18 12:01

    I will certainly say that in most cases, you will see the following signature for callbacks.

    function (err, result)
    

    This is pretty much standard today.

    But it also depends on what you need to "return" like in the createServer example where two objects are passed back to the callback.

    createServer(function (req, res) {
    });
    

    This is mostly the exception and in most libraries you will see the first form.

    0 讨论(0)
提交回复
热议问题