在 Node.JS 中使用 async/await 正确请求 [英] Proper request with async/await in Node.JS

查看:134
本文介绍了在 Node.JS 中使用 async/await 正确请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的程序中,我从另一个 API 模块对我的函数进行 async 调用:

In my program I make async call for my function from another API module:

var info = await api.MyRequest(value);

模块代码:

var request = require("request")

module.exports.MyRequest = async function MyRequest(value) {
    var options = {
        uri: "http://some_url",
        method: "GET",
        qs: {  // Query string like ?key=value&...
            key : value
        },
        json: true
    }

    try {
        var result = await request(options);
        return result;
    } catch (err) {
        console.error(err);
    }
}

执行立即返回,但是 result 因此 info 包含请求对象和请求正文 - info.body 就像 key=value&...,不需要响应正文.

Execution returns immediately, however result and therefore info contains request object and request body - info.body like key=value&..., not required response body.

我做错了什么?怎么修?什么是async 的正确request 用法,或者它仅适用于此处提到的承诺:为什么 await 不适用于节点请求模块?以下文章提到这是可能的:在 Node.js 中掌握异步等待.

What I'm doing wrong? How to fix? What is proper request usage with async, or it only works correctly with promises like mentioned here: Why await is not working for node request module? Following article mentioned it is possible: Mastering Async Await in Node.js.

推荐答案

您需要使用 request-promise 模块,而不是 request 模块或 http.request().

You need to use the request-promise module, not the request module or http.request().

await 适用于返回承诺的函数,而不适用于返回 request 对象并期望您使用回调或事件侦听器来知道事情何时完成的函数.

await works on functions that return a promise, not on functions that return the request object and expect you to use callbacks or event listeners to know when things are done.

request-promise 模块支持与 request 模块相同的功能,但其中的异步函数返回承诺,因此您可以使用 .then()await 而不是 request 模块期望的回调.

The request-promise module supports the same features as the request module, but asynchronous functions in it return promises so you can use either .then() or await with them rather than the callbacks that the request module expects.

因此,安装 request-promise 模块 和然后改变这个:

So, install the request-promise module and then change this:

var request = require("request");

为此:

const request = require("request-promise");

然后,你可以这样做:

var result = await request(options);


编辑 2020 年 1 月 - request() 模块处于维护模式

仅供参考,request 模块及其衍生产品,如 request-promise 现在处于维护模式,不会积极开发以添加新功能.您可以在此处阅读有关推理的更多信息.此表中有一个替代方案列表,其中有一些讨论每一个.

FYI, the request module and its derivatives like request-promise are now in maintenance mode and will not be actively developed to add new features. You can read more about the reasoning here. There is a list of alternatives in this table with some discussion of each one.

我自己一直在使用 got() 并且它已经构建好了从一开始就使用 promise,支持许多与 request() 模块相同的选项,并且易于编程.

I have been using got() myself and it's built from the beginning to use promises, supports many of the same options as the request() module and is simple to program.

这篇关于在 Node.JS 中使用 async/await 正确请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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