我可以同时使用不同的承诺实现吗?

时间:2022-11-25 18:06:18

I've written a script to deploy a web project. It fist uploads a bunch of files via FTP and then sends a request to a chat bot posting a message to https://chat.stackexchange.com/.

我已经编写了部署web项目的脚本。它通过FTP上传一堆文件,然后向聊天机器人发送请求,将消息发送到https://chat.stackexchange.com/。

I'm new to JavaScript and Node.js, and didn't know about promises when I first wrote the code. I'm now in the process of converting it from using nested callbacks to promises with the Node build-in Promise.

我是JavaScript和Node的新手。当我第一次编写代码时,我不知道什么是承诺。我现在正在转换它,从使用嵌套回调到承诺与节点内建承诺。

For making the HTTP request to the bot I've been using request. There's another library called request-promise using Bluebird promises. Are these compatible with the built-in promise implementation? Are there any gotchas I have to look out for?

对于向我一直在使用的bot发出HTTP请求。还有一个叫request-promise的库,用的是蓝鸟承诺。这些与内置的承诺实现兼容吗?我有什么要注意的问题吗?

There's a site listing Conformant Promise/A+ Implementations, but neither Node.js nor Chromium is listed there. Does this mean that I can't use them together?

有一个网站列出了一致性承诺/ a +实现,但是没有一个节点。这里列出了js和Chromium。这是否意味着我不能同时使用它们?

2 个解决方案

#1


2  

You will have to trust the claim that Request-promise is a drop-in replacement for Request

您必须相信请求-承诺是请求的替代

bluebird is a superset of the current built in Promise implementation in node. That is to say that you can use them interchangeably except that bluebird has more features/methods. Rather than try to mix them I would just use bluebird everywhere.

bluebird是当前构建在node的Promise实现中的超集。也就是说,除了蓝鸟有更多的功能/方法,你可以互换使用它们。与其尝试混合它们,我宁愿在任何地方都使用蓝鸟。

If you really don't want to, though, it shouldn't make any difference in terms of chaining promises together. The following still logs hello as expected.

不过,如果你真的不想这么做,那么在把承诺连在一起的问题上也不会有什么不同。以下仍然按预期日志记录hello。

let bluebird = require("bluebird");

new bluebird(resolver => resolver())
.then(() => new Promise(resolver => resolver()))
.then(() => console.log("hello"));

Using Promise = require("bluebird") is pretty common as well.

使用Promise = require(“bluebird”)也很常见。

#2


1  

They are compatible. Probably some implementations differ a little bit, but the main Promise flow is the same. Bluebird seems to be faster even than the native Node.JS implementation.

他们是兼容的。可能有些实现略有不同,但是主要的承诺流是相同的。蓝鸟似乎比本地节点还要快。JS实现。

#1


2  

You will have to trust the claim that Request-promise is a drop-in replacement for Request

您必须相信请求-承诺是请求的替代

bluebird is a superset of the current built in Promise implementation in node. That is to say that you can use them interchangeably except that bluebird has more features/methods. Rather than try to mix them I would just use bluebird everywhere.

bluebird是当前构建在node的Promise实现中的超集。也就是说,除了蓝鸟有更多的功能/方法,你可以互换使用它们。与其尝试混合它们,我宁愿在任何地方都使用蓝鸟。

If you really don't want to, though, it shouldn't make any difference in terms of chaining promises together. The following still logs hello as expected.

不过,如果你真的不想这么做,那么在把承诺连在一起的问题上也不会有什么不同。以下仍然按预期日志记录hello。

let bluebird = require("bluebird");

new bluebird(resolver => resolver())
.then(() => new Promise(resolver => resolver()))
.then(() => console.log("hello"));

Using Promise = require("bluebird") is pretty common as well.

使用Promise = require(“bluebird”)也很常见。

#2


1  

They are compatible. Probably some implementations differ a little bit, but the main Promise flow is the same. Bluebird seems to be faster even than the native Node.JS implementation.

他们是兼容的。可能有些实现略有不同,但是主要的承诺流是相同的。蓝鸟似乎比本地节点还要快。JS实现。