【问题标题】:Handling errors in promise chaining in react-redux application [duplicate]处理 react-redux 应用程序中 Promise 链接中的错误 [重复]
【发布时间】:2017-09-23 15:59:26
【问题描述】:

这是Handling async errors in react redux application的延续

在我的 react-redux 应用程序中

我如何处理我进行 API 调用并且在其成功时我想进行另一个 API 调用并且在其成功时另一个 API 调用的情况?我想处理每次调用的错误。提出一个没有任何副作用和反模式的解决方案。

我的代码

   // sends a request to server 
const makeAsyncCall2 = (arg, response) => {
    const serverArguments = {};

    const req = request
            .post(`${baseUrl}/someotheraction.action`)
            .send(serverArguments)
            .type('form')
            .setAuthHeaders();

    return req.endAsync();
};     

// sends a request to server
const makeAsyncCall1= () => {
    const serverArguments = {};

    const req = request
            .post(`${baseUrl}/somexyz.action`)
            .send(serverArguments)
            .type('form')
            .setAuthHeaders();

    return req.endAsync();
};



   async function makeServerCalls(args, length) {
    // convert args to two dimensional array, chunks of given length [[1,2,3], [4,5,6,], [7,8]]
    const batchedArgs = args.reduce((rows, key, index) => (index % length === 0 ? rows.push([key])
        : rows[rows.length - 1].push(key)) && rows, []);

    const responses = [];

    for (const batchArgs of batchedArgs) {
        responses.push(
            // wait for a chunk to complete, before firing the next chunk of calls
            await Promise.all(
                batchArgs.map((arg) =>
                    request
                        .get(`${baseUrl}/something.cgi`)
                        .query(arg)
                        .endAsync()
                        .then((response) => {
                            makeAsyncCall2(arg, response)
                                .then((res) => console.log(res))
                                .catch((err) => console.log(err))
                            return response;
                        })
                        .catch((error) => {
                            makeAsyncCall2(arg, error)
                                .then((res) => console.log(res))
                                .catch((err) => console.log(err))
                            return error;
                        })
                )
            )
        );
    }

    // wait for all calls to finish
    return Promise.all(responses);
}


// Call starts here 
makeAsyncCall1()
              .then(() => makeServerCalls(args, 3))
              .then((responses) => onAllPromiseResolve(responses));

【问题讨论】:

    标签: javascript ajax reactjs error-handling redux


    【解决方案1】:

    您可以定义一个函数来处理可能的错误,也可以定义多个函数来单独处理每个可能的错误,将.catch() 与错误处理函数作为参数链接到每个Promise 或返回Promise 的函数。

    【讨论】:

    • 你为什么在Promise链之外return responsereturn error
    • 注意,makeAsyncCall2 不是 returned from .then()
    • 我不想返回 makeAsyncCall2 返回的内容,但我想记录 makeAsyncCall2 是否出现问题并防止来自不成功的 makeAsynCall2 和makeAsyncCall1.
    • .catch()makeAsyncCall1()Promise.all(responses)来处理拒绝
    • 我没明白怎么回事?
    猜你喜欢
    • 2018-10-10
    • 2018-03-04
    • 1970-01-01
    • 2018-04-14
    • 2019-06-04
    • 1970-01-01
    • 1970-01-01
    • 2023-03-04
    • 2017-11-28
    相关资源
    最近更新 更多