Ask coding questions

← Back to all posts
nodejs, typewriter effect
WiiU (0)

this has been bugging for the last few weeks, Is it possible to make a type writer effect in nodejs?

Answered by Coder100 (18208) [earned 5 cycles]
View Answer
Voters
WiiU (0)
Comments
hotnewtop
Coder100 (18208)

here is the asynchronous way:

function typeWriter(text, speed = 100) {
  return new Promise((res) => {
    process.stdout.write(text[0], () => {
      if (text.length > 1) setTimeout(() => typeWriter(text.slice(1)), speed);
      else res();
    });
  });
}

typeWriter("hi there");
WiiU (0)

@Coder100

Thanks! I really needed that.

Coder100 (18208)

yeah its much better imo @WiiU

Coder100 (18208)

well yes but actually no

function typeWriter(text, speed = 100) {
  process.stdout.write(text[0], () => {
    if (text.length > 1) setTimeout(() => typeWriter(text.slice(1)), speed);
  });
}

typeWriter("hi there");

you see in node, we do things differently xd
and this way is pretty sucky

tbh you shouldn't use node for these sorts of things idk

WiiU (0)

@Coder100
Thanks for the advice!

Coder100 (18208)

np glad to dissuade help you! @WiiU