I have component in react native that shows all the chats a user has. The main logic must be in componentDidMount(). Here, a simplified version:
componentDidMount(){
ConnectyCube.chat.list({}, function(error, dialogs) {
chats = dialogs.map(chat => {
const opponentId = //some logic
ConnectyCube.users.get(function(error, res){
//some logic to populate chats
});
}
)
this.setState({chats: chats})
}
);
}
Main problem, in other words, is that I don't know how to use multiple callbacks (one for each chat that the user has) to handle the data structure 'chats' in order to setState at the end. Maybe, my problem, is that I'm thinking in a synchronous way because I'm new to an event-driven approach. Any help is appreciated.