在打字稿中的foreach循环内承诺,从响应构造数组并等待所有完成

Promise inside foreach loop in typescript, construct array from response and wait for all done

我正在尝试遍历从 http 调用 return 编辑的套件数组,然后在每次迭代中必须调用 api 以针对每个套件获取分数。每个调用 return 都是一个承诺,我必须构建一个数组并填充每个 api 调用响应的结果。一旦 suites.forEach 循环中的所有内容都完成,我想 return 结果数组。但是当前代码在第一次 suites.forEach 发生后重新运行控制。

import Q = require("q");
public static getTestResults(testcaseId: number): Promise<TestResult[]>{
    
    let deferredPromise = Q.defer<TestResult[]>();
    let results: TestResult[] = [];

    let testManagementRestClient: TestManagementRestClient.TestHttpClient4_1;
    testManagementRestClient = TestManagementRestClient.getClient();
                 
      testManagementRestClient.getSuitesByTestCaseId(testcaseId).then(suites => {
            suites.forEach(suite => {
                testManagementRestClient.getPoints(suite.project.id, +suite.plan.id,
                                                    suite.id, undefined, undefined,
                                                    testcaseId.toString(), undefined,
                                                    true, undefined, undefined
                        ).then(points => {
                            points.forEach(point => {                            
                                    results.push(
                                        {
                                            projectId: suite.project.id,
                                            runId: point.lastTestRun.id,
                                            configuration: point.configuration.name
                                        }
                                    );
                            });
                            deferredPromise.resolve(results);
                        });  
            });
        });
    return deferredPromise.promise;
}

我用谷歌搜索并找到了 $q.all() 但不知道如何在这个特定场景中实现它以及它是否能解决我的问题。

欢迎任何建议!谢谢!

我想你在这里找的是Promise.all。 Assemble 调用您的休息端点作为一组任务,类似于:

const tasks = suites.map(suite => testManagementRestClient.getPoints(...

然后等待所有这些任务解决,使用 Promise.all 如下:

await Promise.all(tasks);

您可以找到更多关于 Promise.all here

来自@Marcus 的解决方案运行良好。这是处理等待所有的另一种方法。

public static getTestResults(testcaseId: number): Promise<TestPoints[]> {
    let deferredPromise = Q.defer<TestPoints[]>();
    let results: TestPoints[] = [];
    let testManagementRestClient: TestManagementRestClient.TestHttpClient4_1;
    testManagementRestClient = TestManagementRestClient.getClient();
    testManagementRestClient.getSuitesByTestCaseId(testcaseId).then(async suites => {
        for (const suite of suites) {
            const points = await testManagementRestClient.getPoints(
                suite.project.id,
                +suite.plan.id,
                suite.id,
                undefined,
                undefined,
                testcaseId.toString(),
                undefined,
                true,
                undefined,
                undefined
            );
            points.forEach(point => {
                results.push(
                    {
                        projectId: suite.project.id,
                        runId: point.lastTestRun.id,
                        configuration: point.configuration.name + " (" + suite.name + ")"
                    }
                );
            });
        }
        deferredPromise.resolve(results);
    });
    return deferredPromise.promise;
}