0

I am using redux. I want to perform a certain network call after updating store. I wrote promise for this as -

let promise=new Promise(function(resolve,reject){
updateStore() //separate method which updates the store.
}).then(()=>{    
 //Netowrk call
 //axios code
});

I am getting a syntax error here : Declaration or statement expected.

What should be the way of performing this activity?

2
  • Please provide a mvce Commented Apr 29, 2022 at 3:09
  • If you don't mind moving it to an async function, you can use await, which will do the Promise magic, but without the additional syntax. Note that async/await isn't compatible with some older browsers. Commented Apr 29, 2022 at 5:21

1 Answer 1

1

You Can but not sure why you would want to use promise here? What are we waiting on?

function updateStore() {
   console.log("updating");
}

let promise=new Promise(function(resolve,reject){
  console.log('update')
  resolve( updateStore() ) //separate method which updates the store.
}).then(()=>{
  console.log('now network')    
 //Netowrk call
 //axios code
});
Using promise

Might be better served just to use a function.

function update_store(callback) {
   console.log("update store");
   callback(); /* or setTimeout( callback ); */
}

function now_network() {
   console.log("update network");
}

update_store(now_network);
Using callback

Sign up to request clarification or add additional context in comments.

2 Comments

network call uses updated store values..thats why we want to wait till store gets updated.
Most network calls have built-in promise-type handlers, so if the updateStore() function can be refactored, it might be as simple as updateStore().then(...) rather than handing it synchronously, then wrapping it asynchronously.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.