I'm using sockets in JS (Node.js) to communicate with a remote host. After I connected to the remote host, the remote host sends me an array of data. When I receive the data, I need to parse the data and store them in my database (using Sequelize). The entries are chained together. So, entry 1 must exist before I can write entry 2 to my database.
The problem is that saving to the database is asynchronous while receiving the data is sychronous. Let's say I receive the first 10 database entries in the "data"-event of the Socket, I parse the first entry and save the entry to the database. Saving to the database is asynchronous and so it's possible that while saving the first entry to the database, the "data"-event happens again (receiving entry 11 till 20). So I parse again and save entry 11 to my database. But when saving entry 11, the previous 10 aren't saved...
How to solve this issue? I already thought about the blukCreate()-function but this doesn't solve the problem because I need any "event" or "point" where I can say something like "wait for async operation before receiving next data". Any idea how to solve this problem?