Seabottom
Seabottom

Reputation: 49

Is there no way to create a delay in node.js?

Basically I have a 3000.txt file that I am removing all vowels from, except if it doesn't contain consonants. The new words are appended to a new TMP.txt file. However, some words are missing entirely, and other words are missing a few consonants. I suspect this is due to the script appending to the file too quickly. Therefore I want to insert a delay of maybe 20 ms or whatever suits. It's just a homegamer script, I don't care if it's efficient.

var fs = require("fs");
var words = fs.readFileSync("3000.txt").toString();
words = words.split("\n");

var vowels = /[aeiouy]/;
var consonants = /[bcdfghjklmnpqrstvxz]/;
var TMPword = "";

for(var testword of words) 
{
    if(testword.match(vowels))
    {
        if(testword.match(consonants))
        {
            TMPword = testword.replace(/a|e|i|o|u|y/g,'')
            fs.appendFile("TMP.txt", TMPword, function (err, file) {if(err) throw err;} );
        }
        continue;
    }

    //Delay goes here//
}

Upvotes: 1

Views: 237

Answers (3)

WildWind
WildWind

Reputation: 162

I'm not sure if a delay would work, but I notice that you use appendFile in a loop, maybe you should use string instead, and call appendFile once when it's done. Otherwise, I suggest that use appendFileSync or await appendFile. Sorry, here's my code example.

var fs = require("fs");
var words = fs.readFileSync("3000.txt").toString();
words = words.split("\n");

var vowels = /[aeiouy]/;
var consonants = /[bcdfghjklmnpqrstvxz]/;
var TMPword = "";

let str = '';

for(var testword of words) 
{
    if(testword.match(vowels))
    {
        if(testword.match(consonants))
        {
            TMPword = testword.replace(/a|e|i|o|u|y/g,'')
            str += TMPword;
        }
        continue;
    }

    //Delay goes here//
}
fs.appendFile("TMP.txt", str, function (err, file) {if(err) throw err;} );

or

fs.appendFileSync("TMP.txt", TMPword);

or

await fs.appendFile("TMP.txt", TMPword, function (err, file) {if(err) throw err;} ); // function must be with async

Upvotes: 2

trincot
trincot

Reputation: 350776

You need to wait until the appendFile callback is called before proceeding with the next call. There are several ways to do that, but this asynchronous loop should do the trick:

var fs = require("fs");
var words = fs.readFileSync("3000.txt").toString();
words = words.split("\n");

var vowels = /[aeiouy]/;
var consonants = /[bcdfghjklmnpqrstvxz]/;

function loop(words) {
    if (!words.length) return; // All done
    var word = words.shift().replace(/a|e|i|o|u|y/g, '');
    fs.appendFile("TMP.txt", word, function (err, file) {
        if(err) throw err;
        loop(words); // Only process next after completing this one
    });
}
// Only retain words that pass the conditions:
words = words.filter(testword => testword.match(vowels) && testword.match(consonants));
// Start asynchronous loop
loop(words);

Upvotes: 2

David Karabin
David Karabin

Reputation: 13

Maybe setTimeout could help you here?

setTimeout(function(){ console.log('foo'); }, 3000);

1000 ms = 1 second;

Or ES6 syntax

setTimeout( ()=> console.log('foo'), 3000 )

Upvotes: 1

Related Questions