Coroutines Generators

suggest change

This uses Bluebird. Promise.promisifyAll adds readFileAsync, and Promise.coroutine turns the generator into a function that returns a promise.

const Promise = require('bluebird');
const fs = Promise.promisifyAll(require('fs'));

const promiseReturningFunction = Promise.coroutine(function* (file) {
  const data = yield fs.readFileAsync(file); // resolves to the file contents

  return data.toString().toUpperCase();
});

promiseReturningFunction('file.txt').then(console.log);

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents