所有的 Node.js 回调函数都是异步的吗? [英] Are all Node.js callback functions asynchronous?

查看:28
本文介绍了所有的 Node.js 回调函数都是异步的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习 Node.js,在每个教程中我听到的都是Node 是异步的,没有阻塞!"

I'm working on learning Node.js and all I hear in every tutorial is "Node is asynchronous and no -blocking!"

我听说在常规浏览器 JavaScript 中,只有某些事情(例如 AJAX 调用)可以异步或非阻塞(使用回调)... Node.js 也是如此,还是所有 Node.js 回调都是如此函数实现异步/非阻塞?

I've heard in regular browser JavaScript only certain things such as AJAX calls can be made asynchronous or non-blocking (using callbacks)... Is this true of Node.js as well, or are all Node.js callback functions made asynchronous/non-blocking?

推荐答案

所有的 Node.js 回调函数都是异步/非阻塞的吗?

are all Node.js callback functions made asynchronous/non-blocking?

没有.通常只有 I/O 是异步的,但许多其他回调是同步的.始终检查文档.

No. Only I/O is usually asynchronous, but many other callbacks are synchronous. Always check the docs.

异步函数示例:

  • 异步文件系统访问(不过他们有没有回调的同步副本)
  • 定时器(setTimeout)
  • process.nextTick, setImmediate
  • 大多数数据库连接
  • 网络连接
  • 承诺
  • Async Filesystem access (they have sync counterparts without callbacks, though)
  • Timers (setTimeout)
  • process.nextTick, setImmediate
  • most database connections
  • network connections
  • Promises

同步回调示例:

  • EventEmitter (depends on when the event is fired)
  • Array iteration methods like forEach
  • Array sort comparator callbacks
  • String replace match callbacks

另见所有的javascript回调都是异步的吗?如果不是,我怎么知道哪些是?(包括其他一些示例).

See also Are all javascript callbacks asynchronous? If not, how do I know which are? (including some other examples).

这篇关于所有的 Node.js 回调函数都是异步的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆