Loop through asynchronous code with events