} function getSecondName() { setTimeout(function(){ gen.next('perry') }, 1000); } function *sayHello() { var a = yield getFirstName(); var b = yield getSecondName(); console.log(a, b); //alex perry } var gen = sayHello(); gen.next(); 1. Next is called on the generator 2. getFirstName() returns undefined but next() is not called until the asynchronous activity has finished. 3. var is then defined as alex 4. getSecondName() is called and the function is again paused until next() is called 5. ‘alex perry’ is logged to the console
the ‘then’ method. fs.readFileAsync('directory/file-to-read') .then(function(fileData){ return fs.mkdirAsync('directory/new-directory'); }) .then(function(){ return fs.writeFileAsync('directory/new-directory/ message.txt'); }) • Then methods can be chained. Much more readable code