I've been working on an angularjs chat module and have come across a challenge.
I developed an algorithm that handles creating new chats, with the following steps:
- Click on the 'New Chat' button
- A list of available people to chat with will be displayed
- Choose a person by clicking on their name, which triggers the 'createChat' function
- 'createChat' then creates a new chat object locally in the JavaScript code and takes you to the chat page
- When submitting a new message, if the 'chat_id' is 0, a new chat is created in the database using a $http post request. Upon success, the 'chat_id' is updated to match the one returned from the HTTP post request
The issue I'm facing is related to asynchronous requests in Angular. Since setting 'chat_id' inside the success function doesn't update it outside of that function, the next message sent still shows a 'chat_id' of 0.
I attempted to research the use of AngularJS $q for handling promises but had trouble grasping how it could help in my situation.