Any reasons to not await for everything and just write sync code?

Hello! I don’t come from the coding background so the whole async/await thing is alien to me. The base concept of functions firing at the same time is clear, however I wonder if anyone can give me some examples of how using async functions makes sense within the realms of Photoshop.

I made some JSX scripts before — that was purely synchronous — and learning UXP now, and for the time being I immediately resolve all the promises with await as soon as I run any async function because the next line often will use the result of this function — basically writing in the same manner as before, just with constant use of the await keyword.

So what are the use cases for using asynchronous code, what am I missing?

Thanks!

You probably find a lot of examples and explanations online if you read about asynchronous functions in Javascript (or generally about parallelism in computing). Broadly speaking I find that a lot of examples evolve around having some process that takes a while to finish or deals with another inherently asynchronous system (e.g. network, etc.), during which you may either continue doing something else or yield back to the caller.

As it comes to Photoshop scripting, in my experience, most cases involving changing Photoshop state (i.e. editing a document) will likely require synchronous steps, since as you said yourself you typically want to continue working on a return value or make sure that you are in sync with the changes. HOWEVER, while these things are happening there’s not much a user can do, i.e. the user is blocked. In a lot of cases you don’t want the user to be able to change anything while your script is working on changing state but there are examples where something could be done in the meantime or where you may be able to yield back to the user. Here’s a concrete (albeit simplified) example.

  • Let’s assume you have a button that takes a snapshot of the Photoshop file and sends it over the network to some trusted server for printing, or processing, etc. and upon completion brings up a dialog to confirm success. Part 1, Taking a snapshot in Photoshop and maybe a bit of processing might not take a whole lot of time and it’s fine if the user is blocked. Part 2, Sending the snapshot to the server and getting it processed may take a few minutes (in this example). It would be nice if the user could get back control over Photoshop and maybe even keep editing the original document, until a confirmation dialog pops up. This would be an example where you could consider wrapping Part 2 in an asynchronous function and calling it without an await, just specifying a callback (for once its promise gets fulfilled). The callback would be the function that pops up the confirmation dialog. What happens in this case would be that the user is blocked while Part 1 happens. Part 2 will yield right after the function call and happen in the background. The user can now do anything they want while stuff happens in the background.
2 Likes