异步等待不起作用

时间:2019-03-11 15:09:35

标签: javascript reactjs asynchronous async-await

我正在使用async等待来异步执行我的程序,但是不知何故。

这是代码。

 loadSessions = async () => {
    //MOCK FIX ME
    try {
        let nextActivities = await loRepositoryApi.get("/user-sessions/next-activities");
        let revisions = await loRepositoryApi.post("/search_revision", { date_programmed: "2019-03-11" });

        let sessionsRevision = []

       await revisions.data.session_id.forEach(async (sId) => {
            const sessionRevision = await loRepositoryApi.post('/get-session', { sessionId: sId })
            sessionRevision.data.isRevision = true
            sessionsRevision = sessionsRevision.concat(sessionRevision.data)
            console.log(sessionsRevision, 'sessionssRev0===')
        })

        console.log(sessionsRevision, 'sessionssRev1===')
        sessionsRevision = sessionsRevision.concat(nextActivities.data);

        console.log(nextActivities.data, 'next-activities===');
        console.log(sessionsRevision, 'sessionssRev2===')

console.log返回了这个。

(28) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}] "next-activities==="
C:\Users\Lucas Navarro\Developer\Alice\alice-platform\Services\AliceMobile\src\screens\home\index.js:160 (28) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}] "sessionssRev2==="
C:\Users\Lucas Navarro\Developer\Alice\alice-platform\Services\AliceMobile\src\screens\home\index.js:153 (29) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}] "sessionssRev0==="

sessionsRev0在另一个日志之后打印,但是应该在之前打印。有人可以帮我吗?

0 个答案:

没有答案