Chapter 17 - Use of a categoryResults array in editAcronymPostHandler


I’m currently at the last part of the Chapter 17 of the book, and there is something I don’t understand in the editAcronymPostHandler(_ req: Request) method. At the 7th comment of the method, it is explained that we create an array to store the category operation results (adding and removal of categories in an acronym), but I don’t understand why we have to store that before the redirection.

@0xtim Can you please help with this when you get a chance? Thank you - much appreciated! :]

@ericlm you need to hold all the futures since the operations to add and remove the relationships are asynchronous. You want to make sure that all the operations are successful and you also can’t leave futures dangling as that can cause memory leaks. So you need to store all the results, wait for them to finish then perform the redirect. Since there could be more than one operation (unlike say, saving a single acronym) it makes it more complex, hence the array. This is one of the prime examples when the code will become a lot easier when Swift gets async/await. Does that makes sense?

Yes, thank you very much ! :+1:t2:

1 Like