javascript - Node event emitter -


i new node , studying event emitter , created 1 demo program

var eventemitter = new event.eventemitter(); var fs = require("fs"); var data = '';  var read = fs.createreadstream("demo.txt"); read.setencoding('utf8');  read.on('data', function(resp) {       console.log(resp); });  data='some data'; var writestream = fs.createwritestream("demo.txt"); writestream.write(data,'utf8'); writestream.end(); writestream.on('finish',function(){     console.log("finish"); }); 

so output 'finish' if write read stream after write stream output 'finish data' why read stream not producing output if put first.

ps: have data in file

as @jfriend00 correctly mentioned, should first write file, can read it.

const fs = require("fs");  var writestream = fs.createwritestream("demo.txt"); writestream.write('some data','utf8'); writestream.end(); writestream.on('finish',function(){   console.log("finish");    let data = '';    const read = fs.createreadstream("demo.txt", {encoding: 'utf8'});    read.on('data', function(resp) {       console.log(resp);     data += resp;   }); }); 

so, several points here:

  • you don't use event emitter in example on own
  • you can set encoding directly in createreadstream method

Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -