当出现网络故障时,我如何重试这些承诺?

How can I retry these promise(s) when there is a network failure?

我有一个使用 Node、React 和 Express 的 create-react-app (CRA)。

当用户登录时,它将像这样启动 React 上下文:

initiateContext = async () => { 

    const promises_1 = [
        this.getBreakthrus(), //check done
        this.getBadges(), //check done
        this.getChapter(), //check done
    ]

    const promises_2 = [
        this.getContext(), //check done
        this.getTeam(), //check done
        this.getTeams(), //check done
        this.getTeamData(), //check done
        this.getFacilitators(), //check done
        this.getNextQuestion(), //check done
    ]

    await Promise.all(promises_1)
    await Promise.all(promises_2);
    this.setLobbyView("profile")
}

这些函数加载应用程序的所有数据,并为用户启动大厅视图。

问题:当用户使用 VPN 连接时,有时 上下文数据不会加载。但是,大厅视图确实加载了,因此用户看到了大厅,但所有数据都是空的。

如何使此功能更灵活,以处理网络故障/重试?

看来您的实际问题是竞争条件。您的代码应该是:

initiateContext = async () => { 


    await Promise.all([
        this.getBreakthrus(), //check done
        this.getBadges(), //check done
        this.getChapter(), //check done
    ]);

    //this will now run AFTER the above
    await Promise.all([
        this.getContext(), //check done
        this.getTeam(), //check done
        this.getTeams(), //check done
        this.getTeamData(), //check done
        this.getFacilitators(), //check done
        this.getNextQuestion(), //check done
    ]);
    this.setLobbyView("profile");
};

此外,semi-colons matter