简体   繁体   English

在 Node.js 中为 function 定义等待

[英]defining a await for the function in Node.js

I am trying to get the response data from 3 APIs which is depended on each other.我正在尝试从 3 个相互依赖的 API 获取响应数据。 when I call each external API the response has an array of external APIs, as I need all the 3 external APIs responses.当我调用每个外部 API 时,响应具有一组外部 API,因为我需要所有 3 个外部 API 响应。 Here is the code:这是代码:

const options = {
    JSON: true,
    method: 'POST',
    headers: {
        'Content-Type': 'application/json; charset=UTF-8',
        "Authorization": process.env.authID,
    },
    body: {},
    uri: ''
};

app.post('/sync', async (req, res) => {
    try {
        let getFirstAPI = Object.assign({}, options);
            getFirstAPI.uri = 'https://abcde';   // first API 
        request(getFirstAPI, function (err, httpResponse, body) {
            Promise.all(body.projects.map((prct) => getVuln(prct))).then(rs => {
                res.JSON({ message: rs });
            });
        });
    }
});
async function getVuln(prodId) {
    try {
        let getSecondAPI= Object.assign({}, options);
        getSecondAPI.uri = 'abcd' + prodId.id + '/abc';  //Second API
        return await new Promise((resolve, reject) => {
          request(getSecondAPI, function (err, httpResponse, body) {
                let final = {
                    products: [],
                    dataList: []
                }
                final.products.push({
                    product_id: prodId.id,
                    product_name: prodId.abc,
                    product_ver: prodId.wdc
                })

                body.issues.forEach(csa => {
                  final.dataList.push({
                        product_id: prodId.id,
                        version: csa.Versions,
                        name: csa.Name,
                        pathJsonValue:await getPathfun(csa.link) //Here i am getting error "SyntaxError: Unexpected identifier"
                    });
                });
                resolve(final);
            });
        })

    }
}
 function getPathfun(urlStr) {
    let getPathUrl = Object.assign({}, options);
    getPathUrl.url = urlStr; 
    return new Promise((resolve, reject) => {
        request(getPathUrl, function (err, httpResponse, body) {
            resolve(body);
        });
    });
}

at the "pathJsonValue" I am getting an error while running the code, if I remove the await then "pathJsonValue" is empty.在“pathJsonValue”处,运行代码时出现错误,如果我删除等待,则“pathJsonValue”为空。 I have attached the error image below.我附上了下面的错误图片。 please help me with this issue.请帮我解决这个问题。 在此处输入图像描述

  1.  request(getSecondAPI, function (err, httpResponse, body) {

Callback must be an ASYNC function to have the possibility to use 'await' keyword inside it.回调必须是 ASYNC function 才能在其中使用“等待”关键字。

  1. 'await' keyword doesnt work in "forEach" method. 'await' 关键字在“forEach”方法中不起作用。 Use for...of loop instead.改用 for...of 循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM