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

188
Views
NodeJS & RaspberryPi - onoff ignoring setTimeout

I am setting up a small node server which can handle the switching on/off of some hard disks I've connected to my RaspberryPi. Now, I wrote a simple handler for the switching off, which should take care of 2 tasks:

  • unmounting the hard disk;
  • switch off the pin;

A non-elegant solution I've found so far, is to execute a command, wait for a certain amount of time, then switch off the hard disk:

const Gpio = require('onoff').Gpio;
const {exec} = require('child_process');
    handlers.switchOff = (data, callback) => {
        if (data.meta.destination != ''
            && data.pin != '') {
            let unmountCommand = "sudo -S umount -f -l " + data.meta.destination;
            let pinOut = new Gpio(data.pin, 'out');
            exec(unmountCommand, (error, stdout, stderr) => {
                    return '';
            });
            setTimeout(function(){
                pinOut.write(0);
                console.log('switched off');
            },10000);
            callback(200, { 'message': 'OK' });
        }
        callback(500, { 'message': 'SERVER ERROR' });
    };

But the behaviour of the setTimeout's filling is unexpected: it executes immediately the pinOut.write(0), while the console.log() is exectued after more or less 10 seconds. Any hint or leads about this behaviour? What am I missing here?

about 4 years ago · Juan Pablo Isaza
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!