简体   繁体   English

异步/等待未按预期执行

[英]Async/Await not executing as expected

I have the below method where I am updating store and after store updating, I am performing certain activities based on store values -我有以下方法更新商店并在商店更新后,我根据商店价值执行某些活动 -

useEffect(()=>{

const asyncUpdateStore=async()=>{
  
  await updateStore();
  getDetails(storeValues)    //this is api call made based on updated store values above
   .then(()=>{...})

}
asyncUpdateStore();

},[applyChange]);

Upon execution of this code, I find that getDetails which is internally a axios call is not waiting for store values to be get updated within updateStore() method.执行此代码后,我发现内部调用 axios 的 getDetails 不等待存储值在 updateStore() 方法中更新。 When useEffect is getting called second time, I find store is updated.当第二次调用 useEffect 时,我发现商店已更新。

I want to wait execution of getDetails, till updateStore method finishes its execution.我想等待 getDetails 的执行,直到 updateStore 方法完成执行。

I have also tried with -我也试过 -

 useEffect(()=>{    
    const asyncUpdateStore=async()=>{      
      await updateStore(); 
    }
    asyncUpdateStore();

getDetails(storeValues)    //this is api call made based on updated store values above
       .then(()=>{...})    
    },[applyChange]);

Edit 1:编辑 1:

updateStore() method involves a dispatch call. updateStore()方法涉及调度调用。

const updateStore=()=>{
 const data:IData={
   value1:valuestate1
   value2:valuestate2
}
dispatch(updateData(data));
}

In redux, all dispatches are synchronous.在 redux 中,所有调度都是同步的。 Using await has no effect there.使用await在那里没有效果。 If updateData() is an asynchronous action, then you may need look at the documentation of the middleware you are using, to handle async actions (ie redux-thunk , etc).如果updateData()是异步操作,那么您可能需要查看正在使用的中间件的文档,以处理异步操作(即redux-thunk等)。

Usually, the middleware will provide 3 states ( "pending", "success", "failed" ) that you can store in your redux store, and then read in your component.通常,中间件会提供 3 种状态( "pending", "success", "failed" ),您可以将其存储在您的 redux 存储中,然后在您的组件中读取。 The logic flow could look like this.逻辑流程可能如下所示。

    //example route that would store the current status of an async response
    const asyncState = useSelector(state => state.storeValues.status)
    const storeValues = useSelector(state => state.storeValues.data)

    useEffect(()=>{     
      //runs everytime applyChange changes
      dispatch(updateData(data));    
    },[applyChange, dispatch, updateData]);
    //runs everytime an async status changes, due to the api request done above
    useEffect(()=>{    
         //success indicates the promise resolved.
         if(asyncState === "success")
            getDetails(storeValues)    //this is api call made based on updated store values above.then(()=>{...})    
     },[asyncState]);

To understand how async patterns work in redux, or see more examples, you can check out redux's own tutorial and docs here .要了解异步模式在 redux 中的工作原理,或查看更多示例,您可以 在此处查看 redux 自己的教程和文档。 The have a conceptual diagram of state flow, as well as a ton of guides.有 state 流程的概念图,以及大量指南。

Note: dispatch actions should never be anything but synchronous, as reducers should not have side effects.注意:派发动作永远应该是同步的,因为 reducer 不应该有副作用。 Redux will throw errors and complain if an action is async, and you may see unexpected behavior in your store if async actions aren't handled outside reducers first.如果一个动作是异步的,Redux 将抛出错误并抱怨,如果异步动作没有首先在 reducer 之外处理,你可能会在你的商店中看到意外的行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM