如何在返回服务器响应的函数中正确格式化嵌套的try / catch块

时间:2019-04-13 00:56:28

标签: javascript typescript async-await

我有一个调用的函数,该函数必须将响应返回给服务器。该函数内部有两个嵌套的await函数调用。为了跟踪错误处理,我添加了try/catch块。有没有一种方法可以避免使用嵌套的try catch块来跟踪该函数可能失败的所有情况,以便我可以发回错误服务器响应?

这是我的功能,它查询用户的唯一设备ID,并向每个用户发送一个推送通知。如果令牌变得无效,则将其从数据库中删除:

function findUserDevices(uid: string, message) {
    collectionData(fb.firestore().collection('devices').where('userId', '==', uid)).pipe(
        filter((userDevices) => userDevices && userDevices.length > 0),
        take(1)
    ).subscribe( async (devices: any) => {
        var userDeviceTokens: string[] = devices.map((device: any) => device.token);
        if (userDeviceTokens !== undefined && userDeviceTokens.length != 0) {
            try {
                message['tokens'] = userDeviceTokens;
                const pushResponse = await admin.messsaging().sendMulticast(message);
                if (pushResponse.failureCount > 0) {
                    const failedTokens = [];
                    pushResponse.responses.forEach((resp, idx) => {
                        if (!resp.success) {
                            failedTokens.push(userDeviceTokens[idx]);
                        }
                    });
                    failedTokens.forEach( async (token) => {
                        var tokenInstanceID = token.split(':')[0];
                        try {
                            await deleteOldToken(tokenInstanceID);
                            console.log(`Token ${tokenInstanceID} deleted`)
                        } catch {
                            return res.status(500).send("err");
                        }
                    })
                    return res.status(200).send("ok");
                } else {
                    return res.status(200).send("ok");
                }
            } catch {
                return res.status(500).send("err");
            }
        } else {
            return res.status(200).send("ok");
        }
    })
}

我必须拥有的所有returns感觉有点过分。我在哪里可以改善?

编辑,将代码分成三个块以防止箭头编码

function findUserDevices(uid: string, message) {
        collectionData(fb.firestore().collection('devices').where('userId', '==', uid)).pipe(
            filter((userDevices) => userDevices && userDevices.length > 0),
            take(1)
        ).subscribe(async (devices: any) => {
            var userDeviceTokens: string[] = devices.map((device: any) => device.token);
            if (userDeviceTokens !== undefined && userDeviceTokens.length != 0) {
                try {
                    message['tokens'] = userDeviceTokens;
                    const response = await admin.messaging().sendMulticast(message);
                    const oldTokensArray = checkOldTokens(response, userDeviceTokens);
                    if (oldTokensArray.length > 0) {
                        await deleteOldTokens(oldTokensArray);
                        return res.status(200).send("ok");
                    } else {
                        return res.status(200).send("ok");
                    }
                } catch (err) {
                    return res.status(500).send(err);
                }
            } else {
                return res.status(200).send("ok");
            }
        })
    }

function checkOldTokens(response, userDeviceTokens) {
    if (response.failureCount > 0) {
        const failedTokens = [];
        response.responses.forEach((resp, idx) => {
            if (!resp.success) {
                failedTokens.push(userDeviceTokens[idx]);
            }
        });
        return failedTokens;
    } else {
        return [];
    }
}

async function deleteOldTokens(tokenArray) {
    for (const token of tokenArray) {
        await fb.firestore().collection('devices').doc(token).delete();
    }
}

0 个答案:

没有答案