AWS X Ray:在 for 循环内,系统不会根据迭代记录每个跟踪 ID 的跟踪。如何在这里使用 AWS Async 功能?

AWS X Ray : inside for loop, system is not logging traces for each Trace ID as per the iteration. How to use AWS Async function here?

我是 javascript 和 AWS SDK 的新手。如何使用异步调用获得预期的输出? 我们有一个 json 对象,它有跟踪 ID 和相关的 URL 等。我们需要遍历跟踪 ID 列表(下面的'id' JSON) 并使用 AWS X-Ray 服务的 getTraceGraph 获取每个跟踪 ID 的跟踪信息。但是当我们检查控制台的日志时,只有在 for 循环迭代完成后,系统才会记录每个跟踪 ID 的跟踪,参考输出。

提前感谢您的帮助。

var aws = require('aws-sdk'); 
aws.config.update({region:'us-east-1'});
var xray = new aws.XRay();
var newObj = {
    "TraceSummaries": [
        {
            "id": "1-some-example",
            "HttpURL": "https://exampleUrl",
            "Name": "name of the service"
        },
        {
            "id": "2-some-example",
            "HttpURL": "https://exampleUrl2",
            "Name": "name of the service"
        }       
    ]
};
var traceArray = newObj.TraceSummaries;
var finalIndex = traceArray.length;
for (i=0; i<finalIndex; i++) {
    console.log('before'+i);
    var singletraceParams = {TraceIds: [traceArray[i].id]}; 
    xray.getTraceGraph(singletraceParams, function(err, data) {
        if (err) console.log(err, err.stack); 
        else {
            var jsonData = JSON.stringify(data);
            console.log('Getting jsonData for'+ ' :'+jsonData);
            console.log('-------------------------------------------------------------');
        }
    }); 
    console.log('after'+i); 
}



实际输出:

before0
after0
before1
after1
Getting jsonData for :{"services":["ReferenceId":0,"Name":"sample1"], "NextToken":null}
-------------------------------------------------------------
Getting jsonData for :{"services":["ReferenceId":0,"Name":"sample2"], "NextToken":null}
-------------------------------------------------------------

预期输出:

before0
Getting jsonData for :{"services":["ReferenceId":0,"Name":"sample1"], "NextToken":null}
-------------------------------------------------------------
after0
before1
Getting jsonData for :{"services":["ReferenceId":0,"Name":"sample2"], "NextToken":null}
-------------------------------------------------------------
after1

您将需要使用promise 来按顺序执行。在带有 promise await 的异步方法中,您可以获得预期的输出

async yourMethod() {
        var xray = new aws.XRay();
        var newObj = {
            "TraceSummaries": [
                {
                    "id": "1-some-example",
                    "HttpURL": "https://exampleUrl",
                    "Name": "name of the service"
                },
                {
                    "id": "2-some-example",
                    "HttpURL": "https://exampleUrl2",
                    "Name": "name of the service"
                }
            ]
        };
        var traceArray = newObj.TraceSummaries;
        var finalIndex = traceArray.length;
        for (let i = 0; i < finalIndex; i++) {
            console.log('before' + i);
            var singletraceParams = { TraceIds: [traceArray[i].id] };
            await new Promise((resolve, reject) => {
                xray.getTraceGraph(singletraceParams, function (err, data) {
                    if (err) {
                        console.log(err, err.stack);
                        reject(err)
                    } 
                    else {

                        var jsonData = JSON.stringify(data);
                        console.log('Getting jsonData for' + ' :' + jsonData);
                        console.log('-------------------------------------------------------------');
                        resolve();
                    }
                });
            })
            .catch(error=>{
                console.error(error);
            })
            console.log('after' + i);
        }
    }