Javascript 您如何正确承诺请求?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28308131/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 01:35:40  来源:igfitidea点击:

How do you properly promisify request?

javascriptpromisebluebird

提问by Madara's Ghost

Bluebird promisifaction is a little magic, and requestis quite a mess (it's a function which behaves as an object with methods).

Bluebird promisifaction 有点神奇,而且request相当混乱(它是一个具有方法的对象的函数)。

The specific scenario is quite simple: I have a request instance with cookies enabled, via a cookie jar (not using request's global cookie handler). How can I effectively promisify it, and all of the methods it supports?

具体场景非常简单:我有一个启用了 cookie 的请求实例,通过 cookie jar(不使用request的全局 cookie 处理程序)。我怎样才能有效地承诺它,以及它支持的所有方法?

Ideally, I'd like to be able to:

理想情况下,我希望能够:

  • call request(url)-> Promise
  • call request.getAsync(url)-> Promise
  • call request.postAsync(url, {})-> Promise
  • 调用request(url)-> 承诺
  • 调用request.getAsync(url)-> 承诺
  • 调用request.postAsync(url, {})-> 承诺

It seems as though Promise.promisifyAll(request)is ineffective (as I'm getting "postAsync is not defined").

似乎Promise.promisifyAll(request)无效(因为我收到“未定义 postAsync”)。

回答by Benjamin Gruenbaum

The following should work:

以下应该工作:

var request = Promise.promisify(require("request"));
Promise.promisifyAll(request);

Note that this means that requestis not a free function since promisification works with prototype methods since the thisisn't known in advance. It will only work in newer versions of bluebird. Repeat it when you need to when forking the request object for cookies.

请注意,这意味着这request不是一个自由函数,因为 promisification 与原型方法一起使用,因为this事先不知道。它仅适用于较新版本的 bluebird。当您需要分叉 cookie 的请求对象时重复它。



If you're using Bluebird v3, you'll want to use the multiArgsoption:

如果您使用的是 Bluebird v3,则需要使用以下multiArgs选项:

var request = Promise.promisify(require("request"), {multiArgs: true});
Promise.promisifyAll(request, {multiArgs: true})

This is because the callback for request is (err, response, body): the default behavior of Bluebird v3 is to only take the first success value argument (i.e. response) and to ignore the others (i.e. body).

这是因为 request 的回调是(err, response, body):Bluebird v3 的默认行为是仅采用第一个成功值参数(即response)并忽略其他参数(即body)。

回答by Arvind Sridharan

you can use the request-promisemodule.

您可以使用request-promise模块。

The world-famous HTTP client "Request" now Promises/A+ compliant. Powered by Bluebird.

世界著名的 HTTP 客户端“Request”现在符合 Promises/A+。由蓝鸟提供支持。

Install the module and you can use request in promise style.

安装模块,您可以使用 promise 样式的请求。

npm install request-promise

回答by Tamlyn

Note that you don't need the third callback parameter, body. It is also present on the responseparameter. If you check the sourceyou can see that bodyis just a convenience for response.body. They are guaranteed to always be the same.

请注意,您不需要第三个回调参数body. 它也存在于response参数中。如果您检查源代码,您会发现这body只是为了方便response.body. 它们保证始终相同。

This means that simple promisification as described in other answers on this page is enough to get all response data.

这意味着本页其他答案中描述的简单承诺足以获取所有响应数据。

const request = require('request')
const { promisify } = require('util')
const rp = promisify(request)

rp('https://example.com').then(({body, statusCode}) => ...)

This is only true of the responsepassed to the callback/promise. The responseobject passed to the response eventis a standard http.IncomingMessageand as such has no bodyproperty.

这仅适用于response传递给回调/承诺的信息。response传递给响应事件的对象是标准的http.IncomingMessage,因此没有body属性。

回答by Little Roys

I give an example, by utilbase on Node.js v11.10.0

我举个例子,util基于Node.js v11.10.0

import { get, post } from "request";
import { promisify } from "util";

const [getAsync, postAsync] = [get, post].map(promisify);


getAsync("http://stackoverflow.com")
    .then(({statusCode, body}) => { 
       //do something 
     });

Or, equivalently using async/await:

或者,等效地使用async/await

const foo = async () => {
    const {statusCode, body} = await getAsync("http://stackoverflow.com")
    // do something
}