How to wait until writeToFile: method competes writing?

Hello, friends.

Often I use the writeToFile: method like here:

set theArchiveData to webFrame’s dataSource()'s webArchive()'s |data|()
theArchiveData’s writeToFile:thePath atomically:true
– the next code line

As I understand, the AppleScript interpretator continue with next code line. That is, doesn’t wait for writing completion. So, how to make it to wait?

That call shouldn’t return until the write is complete.

Thanks for the clarification.

I have one script that uses a repeat loop with this method inside it. After about 50-400 iterations, the script hangs.

I assumed that the reason for the hang is in this method, but as you explained to me, now I understand that the reason is something else.

I thought they were depricating this for writeToURL
As I was going to suggest that.
But they haven’t :
https://developer.apple.com/documentation/foundation/nsdata/1408033-writetofile

You’ll notice that it returns a BOOL value to let
You know if the save worked.
You might want to add that to your code,
You’ll know when it completed and if it was
Successful.