Node.js Asynchronous Function *Definition*

前端 未结 4 845
失恋的感觉
失恋的感觉 2020-12-30 15:16

Please, just to clear something up in my head...

I am used to writing using asynchronous functions in libraries, but how do I write my own?

To illustrate my

4条回答
  •  青春惊慌失措
    2020-12-30 16:05

    Asynchronicity in NodeJS processes happens when you delegate some work to external processes (or probably some work in Node's C layer), that are e.g. database queries, file system operations.

    Usually there's no asynchronicity in plain JavaScript functions (Node.js is single threaded) there are not many valid cases for that. However, if it's really heavy, and you really want to do that, you may either delegate your work to other process (see child_process.fork) or split operations with help of setTimeout (to give process a breathe to handle other request), then indeed it should be configured as asynchronous function.

提交回复
热议问题