Quantcast
Channel: Active questions tagged react-native+typescript - Stack Overflow
Viewing all articles
Browse latest Browse all 6287

Async does wait for data to be returned in a redux-thunk function

$
0
0

I've being trying populate my redux store with data that comes from my mongo-db realm database.Whenever I run the function below it will execute fine but the problem is data will be delayed and ends up not reaching my redux store.

My thunk function:

export const addItemsTest = createAsyncThunk("addItems",  async (config: any) => {    try {      return await Realm.open(config).then(async (projectRealm) => {        let syncItems = await projectRealm.objects("Item");        await syncItems.addListener((x, changes) => {          x.map(async (b) => {            console.log(b);            return b;          });        });      });    } catch (error) {      console.log(error);      throw error;    }  });

and my redux reducer:

  extraReducers: (builder) => {      builder.addCase(addItemsTest.fulfilled, (state, { payload }: any) => {        try {          console.log("from add Items");          console.log(payload);          state.push(payload);        } catch (error) {            console.log(error)         }      });  }

Expected Results:My redux store should have these data once addItemsTest return something:

[{itemCode: 1,itemDescription: 'Soccer Ball',itemPrice: '35',partition: 'partitionValue',},{itemCode: 2,itemDescription: 'Base Ball',itemPrice: '60',partition: 'partitionValue',}]

Actual Results:Console Results


Viewing all articles
Browse latest Browse all 6287

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>