Async TextBuffer.save Atom 1.19 - How to make pane.saveItems() finish before proceeding?


#1

In line 27 here
https://github.com/h-a-n-n-e-s/kara/blob/master/lib/kara.coffee
I need to make sure that saveItems() finishes saving all files before continuing, because later a Makefile is invoked which checks if files have been changed. Because of the new asynchronous save of the TextBuffer in Atom 1.19 the files seem not to be saved yet when the Makefile is invoked.
How can I make sure that all files are saved before continuing the script?


#2

There’s no way that you can do that with the current implementation of Pane.saveItems because while Pane.saveItem returns a Promise that resolves when the item is saved, saveItems doesn’t wait on that Promise or return a compound Promise. You would have to iterate over the items in the pane, call saveItem on each one and wait on all of the returned Promises.


#3

Thank you.
Ok, so I now replace
currentPane.saveItems()
by
for item in currentPane.getItems()
currentPane.saveItem(item)
But how I wait for the returned promises I couldn’t figure out. Is there a standard way to do this?
Also, different to save.Items() now the saveItem(item) also seems to touch all the files which were not modified so the Makefile later thinks all the files have been changed, which goes against the whole idea.


#4

I remember this article was one of the most helpful ones to help me understand Promises.


#5

I somehow easily understand this article
https://hackernoon.com/6-reasons-why-javascripts-async-await-blows-promises-away-tutorial-c7ec10518dd9
But await does not seem to work yet in this dreadful Coffeescript.
So how do I get something like await currentPane.saveItem(item) to work?


#6

Generally speaking, whenever Atom doesn’t support a language feature, you can use Babel or TypeScript and transpile your package on install-time using a postinstall script. If you want more control over the transpiled output, I guess using prepublish works just as well.

Example for the TypeScript compiler:

"devDependencies": {
    "typescript: "^2.5.2"
},
"scripts":
    "postinstall": "./node_modules/.bin/tsc -p ./"
}

See the scripts documentation for details.

Update: CoffeeScript 2 supports async/await, so you can use it as I described the process for TypeScript.


#7

I really appreciate your help @idleberg, but unfortunately I do not have the time to dig into all these things. Somehow I find the publishing policies of the Atom team quite weird. Something which was a single short line before now with this async TextBuffer.save becomes some lengthy stuff with syntax which is not even fully implemented in the current version, so I need to install some other packages and run scripts to do whatever… ? Sorry, just feels wrong. I now wrote a very dirty wait-a-short-time-hack which somehow works. Maybe I’ll have a look at it again once this promise hype crap is over and async/await works properly out of the box. :expressionless:


#9

Under the hood, Promises and async/await are the same thing, the latter is a much better syntax pattern for the former. Every async function returns a Promise. Bottom line is that it doesnt hurt understanding Promoses, it could be essential when debugging more complex async/await usage.