How to save a file on window close event

33 views
Skip to first unread message

Kevin Wagner

unread,
May 8, 2015, 12:13:32 AM5/8/15
to appj...@googlegroups.com

Hi All,

Currently the application I am building has a table which needs to be saved to the users computer before the application is closed. My first thought was to call the save function inside a window.on("close") like so.

window.on('close', function(){
   
var data = "Some data gathered from the table"
   
var fs = require('fs');
    fs
.writeFile(__dirname + "/content/SomeFile.txt", ""+data+"", function(err) {
       
if(err) {
            window
.console.log(err)
       
} else {
            window
.console.log("success")
       
}
   
});
})

This however always results in SomeFile.txt being empty which leads me to believe the problem is that the program closes before it gets a chance to finish saving the file. Is there some way to either catch and cancel this close event until after the file has finished saving. If not is there some other way I have not thought of?
Reply all
Reply to author
Forward
0 new messages