javascript - Redux Thunk handling network responses in a different order -


i have text entry field (via thunk) queries server of validity of user's text. these requests can happen in quick succession, may returned server in different order when sent. therefor, string in text field may shown invalid, when in fact valid.

to fix this, i'm performing check when receive response server - current content of text field same checked? if not, check again. feel there should better way handle situation querying dom element value.

how can follow through pre post server request?

export function updateusercode(code) {     return dispatch => {         return dispatch(validateusercode(code))     } }  function validateusercode(code) {     return dispatch => {         dispatch(updatecode(code))         return fetch(`/api/code/${code}`)             .then(response => response.json())             .then(json => dispatch(receivevalidatedcode(code, json)))             .catch(error => {log.error(error)})     } }  function receivevalidatedcode(code, data) {     const lastval = document.getelementbyid('usercode').value     if (code != lastval) {         // code not same current value         // need validate again         return updateusercode(lastval)     }     if(data.success) {         return {             type: types.code_validated,             code: code,         }     }     return {         type: types.code_invalid,         reason: data.reason,     } } 

messing dom inside logic indeed less ideal. suggest keeping last entered text field value in redux store , perform checks in reducer.

also don't see point in re-validating user input if current entered value differs 1 validated last resolved request. ignore such responses , not perform unnecessary request.

in terms of code can that:

// actions const requestvalidation = value => ({ type: 'request-validation', value });  const receivevalidation = (value, result) => ({ type: 'receive-validation', value, result });  export const validateusercode = code => dispatch => {   dispatch(requestvalidation(code));   return fetch(`/api/code/${code}`)          .then(response => response.json())          .then(json => dispatch(receivevalidation(code, json)))          .catch(error => {log.error(error)}) }  // reducer export const validationreducer = (state = {}, action) => {   if (action.type === 'request-validation')      return ({ value: action.value, isvalidating: true });    if (action.type === 'receive-validation' && state.value === action.value)      return ({ value: action.value, isvalid: !!action.success });    return state;  }; 

that not production quality code, , don't event sure if works, reflects idea.


Comments

Popular posts from this blog

inversion of control - Autofac named registration constructor injection -

verilog - Systemverilog dynamic casting issues -

ios - Change Storyboard View using Seague -