- Mastering Node.js(Second Edition)
- Sandro Pasquali Kevin Faaborg
- 175字
- 2021-07-02 19:28:45
Callbacks
Luckily, Node creators agreed upon sane conventions on how to structure callbacks early on. It is important to follow this tradition. Deviation leads to surprises, sometimes very bad surprises, and in general, to do so automatically makes an API awkward, a characteristic other developers will rapidly tire of.
One is either returning a function result by executing a callback, handling the arguments received by a callback, or designing the signature for a callback within your API. Whichever situation is being considered, one should follow the convention relevant to that case.
The first argument returned to a callback function is any error message, preferably in the form of an error object. If no error is to be reported, this slot should contain a null value.
When passing a callback to a function, it should be assigned the last slot of the function signature. APIs should be consistently designed this way.
Any number of arguments may exist between the error and the callback slots.
To create an error object: new Error("Argument must be a String!")
- 通信網絡基礎與設備
- 面向物聯網的CC2530與傳感器應用開發
- WordPress 5 Complete
- PLC、現場總線及工業網絡實用技術速成
- 企業私有云建設指南
- 網絡環境中基于用戶視角的信息質量評價研究
- 語音信號處理及Blackfin DSP實現
- INSTANT LinkedIn Customization How-to
- Laravel Application Development Cookbook
- 區塊鏈技術與應用:打造分布式商業新生態
- 網絡互聯技術(理論篇)
- LiveCode Mobile Development Beginner's Guide
- Scala Programming Projects
- WSN RFID物聯網原理與應用
- 計算機聯鎖及信號微機監測系統