简体   繁体   中英

Node.js and Synchronous API functions

Node.js usually encourages usage of asynchronous API functions and callbacks. Is it accurate to say that if the function name includes the word "Sync", so this function is a synchronous function?

For example, in this link: https://nodejs.org/api/fs.html , you can see both fs.writeFile(file, data[, options], callback) and fs.writeFile Sync (file, data[, options]). The former is a-synchronous and the latter is synchronous.

Yes, in NodeJS' API(s), functions that contain "Sync", are typically synchronous versions of asynchronous functions.

Edit : Please see StackOverflow user jfriend00's answer , as they went into further detail about how to tell if a function is synchronous or asynchronous in NodeJS' API(s).

Is it accurate to say that if the function name includes the word "Sync", so this function is a synchronous function?

Yes, that is accurate.

Here's how you tell if something is synchronous or asynchronous in the node.js API:

  1. If it ends with a suffix of "Sync", then it is synchronous.
  2. If it's any kind of I/O operation and it accepts a callback and does not have "Sync", then it's asynchronous.
  3. If there are two options offered and one ends with a suffix of "Sync", then the other one is asynchronous.
  4. If it does not accept a callback or return a promise or use an eventEmitter (or something similar) to provide event notifications, then it is not asynchronous because there would be no way for it to communicate to you when it's done.
  5. You consult the documentation for the specific function and look for a reference to asynchronous.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM