0

this is the first one of a series of questions i want to submit: in a given point of a es6 js book ,reading a file is done by the aid of new yield token,but some syntax is odd to me can you explain me whats happening under the hood?

run(function*()
let contents=yield readFile("config.json");
dosomething(contents);
console.log("done");
});

Till now its was done async by:

let fs=require("fs")

function readFile(filename){
 return function(callback){
 fs.readFile(filename,callback);
  };
 };

it claims same scheme (first one) can do sync and async loading of file but it seems that adds some node.js i does not understand In the second form this is a'functional closure' how does it work or its called?

Michał Perłakowski
  • 88,409
  • 26
  • 156
  • 177
FJsanto
  • 3
  • 6
  • 2
    The first one is the deprecated workaround for the new `async` / `await` syntax. – Jonas Wilms Feb 12 '18 at 17:08
  • 1
    See [What's the yield keyword in JavaScript?](https://stackoverflow.com/q/2282140/3853934) – Michał Perłakowski Feb 12 '18 at 17:09
  • 1
    @JonasW. Apparently the `run` function for the generator expects thunks that take a callback, and `readFile` was generating such one. It's probably explained in the chapter about the `run` function (if it is not, that book should be thrown away) – Bergi Feb 12 '18 at 18:54
  • thx Bergi i continue reading – FJsanto Feb 13 '18 at 16:04

0 Answers0