Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

379
Views
FileSystem, File Not Written To, No Errors NodeJS

I'm attempting to write to a .txt file using the FileSystem module NodeJS provides. However, this it is not running and it's not showing any errors. Upon further expection, I can also tell that read/writeFileSync's callback is not being ran.

utils.js

    writeFile(path,encoding)
    {
        console.log('debug write')
        fs.writeFileSync(path,encoding,(err)=>{
            if(err) console.log(this.errorCodes.state.err004);
            else console.log('success');
            return;
        });
        return;
    }
    readFile(path,encoding)
    {
        console.log('debug read')
        return fs.readFileSync(path,encoding,(err)=>
        {
           if(err) console.log(this.errorCodes.state.err004) 
        });
    }

state.js

//returns data from state
    async get(key)
    {
        //root not updating to interact with state
        this.utils.readFile('./tempRoot.txt','utf8');
        console.log(this.root)
        const value = await this.trie.get(Buffer.from(key));
        console.log(value.toString())
        
        return 
    }

//sets data in state
    async set(key,value)
    {
        await this.trie.put(Buffer.from(key),Buffer.from(value));
        this.setRoot();
        return;
    }

    //set root of state
    setRoot()
    {
        const root = this.getRoot().toString('hex');
        
        console.log(root)
        this.utils.writeFile('./tempRoot.txt',root);
    }

My guess is that the synchronous operation is being called in an async function but I don't understand why that would have an effect as it would just await then execute the file write/read functions?

Questions: What am I doing wrong? And if it's the async function, then how would that have an impact?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

As I don't have enough reputation to comment, I'll link the answer: fs.writeFileSync doesn't take a callback

fs.writeFileSync function does not take an argument to pass a callback function, rather it throws an error to be caught; see the documentation: fs.writeFileSync

Callback functions run after another function has finished its execution. Such waiting is needed for asynchronous functions, that execute independently. Synchronous functions, on the other hand, block the control flow, eliminating the need for callback functions.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!