需要找到所有回调何时解决

need to find when all callbacks resolved

下面是我的 javascript 函数的一个片段。它采用任意大小的对象数组 travelDataGroups[{}] 并对其进行迭代。在 forEach() 内部,我必须调用异步的 newUser(),在内部我必须调用异步的 loadOtherUsersPayRate()
解决所有回调并完成循环后,我需要调用 makeWorkbookDownloadObj(),问题是它只能调用一次,否则会损坏对象。

myGroupWorkReportObj.travelDataGroups.forEach( travelerData =>
{
    // add the pay rate for this user
    const someUser = new User( travelerData.employeeId , function()
    {
        // add the pay rate for this user and project
        const projectId = travelerData.projectId;
            
        const payRate = loadOtherUsersPayRate( projectId, someUser )
            .then( payRate =>
            {
                //create a row of travel data
                const rowOfData = [
                    travelerData.firstName,
                    travelerData.lastName,
                    travelerData.travelHours,
                    financial( payRate.pay / 2 )
                ];

                // push the row of data into the sheet
                thisSheet.data.push( rowOfData );

                // need to only call this once, when all callbacks are resolved
                // makeWorkbookDownloadObj( workbookObj );
        });
});

编辑最终工作代码:

const userPromises = [];

// wrap the User class in a Promise
var aUserPromise = ( travelerData ) =>
{
    return new Promise( ( resolve, reject ) =>
    // new User( employeeId, ( someUser ) =>
    {
        // return new Promise( ( resolve, reject ) =>
        new User( travelerData.employeeId, ( someUserObj ) =>
        {
            // add the pay rate for this user and project
            loadOtherUsersPayRate( travelerData.projectId, someUserObj )
            .then( payRate =>
            {
                //create a row of travel data
                const rowOfData = [
                    travelerData.firstName,
                    travelerData.lastName,
                    travelerData.travelHours,
                    financial( payRate.pay / 2 )
                ];

                // push the row of data into the sheet
                thisSheet.data.push( rowOfData );

                resolve();
            } );
        } );
    } );
};

myGroupWorkReportObj.travelDataGroups.forEach(( travelerData ) =>
{
    // add the pay rate for this user
     ** this line would push the function, but not execute it (until later),
     ** so did in two lines as below
    // userPromises.push(aUserPromise(travelerData)); 

    var myRun = aUserPromise( travelerData );
    userPromises.push( myRun );
});

Promise.allSettled( userPromises )
.then( ()=>
{
    makeWorkbookDownloadObj( workbookObj )
});

我这样做 - 使用 Promise.all - 请参阅此处获取文档:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all

请注意以下代码未经测试,但您可以了解如何操作:

  • 创建承诺数组
  • 对该数组调用 Promise.all

const promises = []

promises.push(
    loadOtherUsersPayRate( projectId, someUser )
            .then( payRate =>
            {
                //create a row of travel data
                const rowOfData = [
                    travelerData.firstName,
                    travelerData.lastName,
                    travelerData.travelHours,
                    financial( payRate.pay / 2 )
                ];

                // push the row of data into the sheet
                thisSheet.data.push( rowOfData );

                // need to only call this once, when all callbacks are resolved
                // makeWorkbookDownloadObj( workbookObj );
        });
)

Promise.all(promises).then( ()= > makeWorkbookDownloadObj( workbookObj ));

按照您的评论并使用 Promise ALL 和上一个答案使用的相同模式:

const userPromises = [];
myGroupWorkReportObj.travelDataGroups.forEach( travelerData =>
    {
// add the pay rate for this user
        userPromises.push(new User( travelerData.employeeId , function()
        {
            // add the pay rate for this user and project
            const projectId = travelerData.projectId;
    
            const payRate = loadOtherUsersPayRate( projectId, someUser )
                .then( payRate =>
                {
                    //create a row of travel data
                    const rowOfData = [
                        travelerData.firstName,
                        travelerData.lastName,
                        travelerData.travelHours,
                        financial( payRate.pay / 2 )
                    ];
    
                    // push the row of data into the sheet
                    thisSheet.data.push( rowOfData );
                })
        }));
    });
Promise.all(userPromises).then(() => {
    makeWorkbookDownloadObj( workbookObj );
});

真正需要解决的承诺是用户的创造。