I think you just need to declare Start_Flash up top so itās in scope. The yield syntax is just for generator functions. Also, your yields are awaiting same time as total interval, so may not produce expected behaviour - it reads like it will set red and white close to simultaneously.
So there isnāt an āEasyā way to cancel a coroutine in BJs, but a āreturnā if statement after every await will cause a breakout at anytime though.
I think I got it:
I was looking for an external way to stop it, like in Unity: stopCoroutine(Start_Flash) or JS: clearInterval(Stop_Flash); so your answer threw me off coarse, I didnāt know where to put the return. Iām not sure why they didnāt add something like in Unity, maybe in the next upgradeā¦
It wonāt cancel a running method with containing awaits (unless you have a breakout state as you have done). For equivalent to unity coroutine have a look at Javascript generators: * function() {...}
That functionality is already part of ES6 - you can read more on how they work here: The Basics Of ES6 Generators (davidwalsh.name)
@charliesideroad you are actually not using coroutines here but only promises.
@syntheticmagus and @ryantrem added support for coroutines in babylon with support for cancellation and can probably detail a bit more their work here ???
The easiest way to cancel coroutines is using the cancelAllCoroutines call. Iām not 100% sure I understand what your scenario is trying to do, but this API is made available specifically for stopping coroutines.
Reiterating what others have said, your code as shown isnāt using any kind of coroutines, just promise based async functions. In your example code in the post, you use setInterval, in which case calling clearInterval seems reasonable. You could also have the Flash_Whites function just run your code in a loop, and have another mechanism to cancel the async code running in the loop. AbortController from WebAPI is typically used for this.
You could also use an actual Babylon coroutine, and one way of doing the example provided by @sebavan. That is the lower level coroutine system of Babylon.js. You can also use a higher level coroutine layer based on observables, and cancelling that type of coroutine is done with the function @syntheticmagus mentioned. Here is @sebavanās example modified to use the observable coroutine layer: colorChange | Babylon.js Playground (babylonjs.com)