- Run
useForLoop
independently, and observe it takes about 7 seconds to complete - Run
entrypoint
which calls useForLoop three times, asynchronously - Observe that the second run takes at least three times as long as the first run to complete
- Repeat the process above but in a browser, and observe the expected behaviour
Conclusion: Even though the documentation indicates that you can define async functions, and that you can make Promises, it ain't actually async.
Even more proof is in the fact in observing what happens when you run in the browser. It executes entrypoint()
immediately and "finishes" (although the calculations are still being made) and eventually outputs. But for the V8 engine, it indicates to the user that it is running and doesn't immediately return to the user.
- Why is
setTimeout
undefined butPromises
andAsyncFunction
work as expected (minus the actual async) - Will Google have another implementation where they do work?
Please see this gist to see the only way to do async on AppsScripts platform. (Note: It now requires you to create your own GCP project and link new project to that manually.)
So I apologize I didn't quite understand your code, and you might want to make some bug fixes as it doesn't work as-is atm.
I guessed at what you were trying to accomplish and reduced it down a bit. You can see from the numbers a pure asynchronous workload is magnitudes faster then a synchronous one. I verified the code against Node v12.14.1 and got similar timings.
The part I don't understand yet is when you mix synchronous and asynchronous code. The async workload seems to get locked with the sync work load. But you will see this is not the way Apps Script "implements" promises. This is how the V8 engine is currently working.
EDIT : I have a suspicion it has to do with 'console' in this case.