了解node.js中的promise [英] Understanding promises in node.js

查看:108
本文介绍了了解node.js中的promise的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据我的理解,有三种方法可以调用异步代码:

From what I have understood there are three ways of calling asynchronous code:


  1. 事件:例如。 request.on(event,callback);

  2. 回调:例如。 fs.open(路径,旗帜,模式,回调);

  3. 承诺

我找到了一个承诺库 https://github.com/kriszyp / node-promise 但我不明白。

I found a promise library https://github.com/kriszyp/node-promise but I don't get it.

有人可以解释一下所有的承诺以及为什么要使用它吗?

Could someone explain what promises are all about and why I should use it?

另外,为什么它从Node.js中删除了?

Also, why was it removed from Node.js?

推荐答案

节点中的Promise .js承诺做一些工作然后有单独的回调,这些回调将被执行成功和失败以及处理超时。在node.js中考虑promises的另一种方法是它们是只能发出两个事件的发射器:成功和错误。

Promises in node.js promised to do some work and then had separate callbacks that would be executed for success and failure as well as handling timeouts. Another way to think of promises in node.js was that they were emitters that could emit only two events: success and error.

关于promises的一件很酷的事情就是你可以结合起来它们进入依赖链(仅当Promise A Promise B完成时才做Promise C。)

The cool thing about promises is you can combine them into dependency chains (do Promise C only when Promise A and Promise B complete).

通过从核心node.js中删除它们,它创造了构建具有不同承诺实现的模块的可能性,这些承诺可以位于核心之上。其中一些是 node-promise 期货

By removing them from the core node.js, it created possibility of building up modules with different implementations of promises that can sit on top of the core. Some of these are node-promise and futures.

这篇关于了解node.js中的promise的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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