Question from the Node.js test

Use ES2015 generators to display the contents of a file in NodeJS

Hard

We want to use the ES2015 generators to display the contents of thehello.txt file:

const fs = require('fs');

function run(generator) {
  function callback(err, content) {
    console.log(`1) Data read : “${content}”`);
    iterator.next(content);
  }

  var iterator = generator(callback);
  iterator.next();
}

run(function *(callback) {
  var content = yield fs.readFile('hello.txt', callback);
  console.log(`2) The file contains : “${content}”`);
});

What will happen if we run this code?
(Assuming that hello.txt exists and that it contains the text: Hello World!)

Author: BenjaminStatus: Published(Update)Question passed 78 times
Edit
0
Community EvaluationsNo one has reviewed this question yet, be the first!