Wait for Arduino IoT cloud to finish connecting

Have you tried putting the code you want to run, only after it's connected, in the else section and within the void loop? Then it should only run after its fully connected.