Running in node.js, whenever I do something like
setTimeout(process.stdout.write, 200, "Test")
I get the following error:
Cannot read property 'defaultEncoding' of undefined
I have tried to set
setTimeout(process.stdout.write.bind(this), 200, "Test")
Still not working
When you call process.stdout.write(...)
, the context (this
) inside of it is process.stdout
. When you pass it to setTimeout
, this context gets lost. As you noticed, bind
is the correct way to fix this - but you need to bind to the right thing:
setTimeout(process.stdout.write.bind(process.stdout), 200, "Test")