最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - writeFileSync doesn't callback - Stack Overflow

matteradmin8PV0评论

I am using writeFileSync function to write a file locally, the file does get written, however, the callback function is never called.

I did some googling, some other post are having the issue that it's either 1) passing the content went wrong or 2) having two write function at the same time.

My problem is that there are some other places in my code that is using the writeFileSync, but they are on different routes (not sure if this is the right terminology, localhost:port#/differentroutes <- something like this). I am testing only on my own route so those write functions shouldn't even be called.

Here is my code:

if(!fs.existsSync(dir)){
        fs.mkdirSync(dir)
    }

//content is just a string var I swear it's just a string

    fs.writeFileSync('./pages/SubmissionProcess.html',content,function(err){
        if(err){
            throw err
        }else {
            console.log("YES")
        }
    })

I never see this "YES" nor error in my console even tho the file is already written....

I am using writeFileSync function to write a file locally, the file does get written, however, the callback function is never called.

I did some googling, some other post are having the issue that it's either 1) passing the content went wrong or 2) having two write function at the same time.

My problem is that there are some other places in my code that is using the writeFileSync, but they are on different routes (not sure if this is the right terminology, localhost:port#/differentroutes <- something like this). I am testing only on my own route so those write functions shouldn't even be called.

Here is my code:

if(!fs.existsSync(dir)){
        fs.mkdirSync(dir)
    }

//content is just a string var I swear it's just a string

    fs.writeFileSync('./pages/SubmissionProcess.html',content,function(err){
        if(err){
            throw err
        }else {
            console.log("YES")
        }
    })

I never see this "YES" nor error in my console even tho the file is already written....

Share Improve this question asked Oct 5, 2018 at 18:10 AnnaAnna 5719 silver badges31 bronze badges 9
  • Cos it's sync duh, dont need to register a callback!!!! – shanks Commented Oct 5, 2018 at 18:12
  • @shanks what does that mean... – Anna Commented Oct 5, 2018 at 18:12
  • You only register callbacks for async functions, sync versions dont need callbacks – shanks Commented Oct 5, 2018 at 18:13
  • fs.writeFileSync('./pages/SubmissionProcess.html',content) should surffice – shanks Commented Oct 5, 2018 at 18:14
  • @shanks okay...so how do I know it finished??? – Anna Commented Oct 5, 2018 at 18:14
 |  Show 4 more ments

2 Answers 2

Reset to default 3

Write file sync doesn't take a callback :D

Take a look at the documentation :

https://nodejs/api/fs.html#fs_fs_writefilesync_file_data_options

The parameters are (path, data, options)

If you want to check if the file actually wrote, you can read file sync after writing it or check the last time the file was updated. Otherwise, you should try using the async approach.

All of the synchronous methods throw rather than passing an error to a callback.

try {
    fs.writeFileSync('./pages/SubmissionProcess.html', content);
    console.log('YES');
} catch (e) {
    console.error(e);
}
Post a comment

comment list (0)

  1. No comments so far