Alexito's World

A world of coding 💻, by Alejandro Martinez

Wait for async code in a Swift playground

By default a Swift playground stops it's execution when the top level function finishes. If you want to run async code you will never see the results. The system doesn't provides a waiting architecture like node.js, which is a bad thing scripts.

But in the playground you can continue running the main loop even if the top level funcitons has finished. You just need to import XCPlayground framework and call XCPSetExecutionShouldContinueIndefinitely(). This will continue running the playground until the time out (by default 30s). You can change that time out in the assistant editor.

If you liked this article please consider supporting me