1

I would like to know how can I call a function after all the webSockets messages are sent and the response is received.

Technically what I am trying to do is to save all the data in a variable inside a component and at the end to call set state with that variable

event.markets.forEach( (marketId, index) => {
  console.log('index: ', index)
  sendMessage(messageTypes.getMarketType, {id: marketId})

})

this.props.setMarkets()   
  public setMarkets = () => {
    this.setState({
      markets: this.markets
    })
  }

The problem is setMarkets() is called before the messages are received.

the onmessage function goes here:

stateManagement = data => {
  this.markets = {
    ...this.markets,
    [data.data.eventId]: [data.data]
  }
}

and it's set like this

w.onmessage = data => this.stateManagement(JSON.parse(data.data))

Anybody has any ideas please?

Rastalamm
  • 1,712
  • 3
  • 23
  • 32

0 Answers0