Last active
September 18, 2015 12:07
-
-
Save saintedlama/c99c5208c72713e34c9c to your computer and use it in GitHub Desktop.
A better co example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
const co = require('co'); | |
const fs = require('fs'); | |
co(function* () { | |
let readFile = (resolve, reject) => { | |
fs.readFile('./codemo.js', { encoding : 'utf-8' }, function(err, content) { | |
if (err) { | |
return reject(err); | |
} | |
resolve(content); | |
}); | |
}; | |
var res = yield [ | |
new Promise(readFile), | |
Promise.resolve(1), | |
Promise.resolve(2), | |
Promise.resolve(3), | |
]; | |
console.log('res1', res); | |
var res2 = yield [ | |
new Promise(readFile), | |
Promise.resolve(1), | |
Promise.resolve(2), | |
Promise.resolve(3), | |
]; | |
console.log('res2 - always executed after res1', res2); | |
}).catch(onerror); | |
function onerror(err) { | |
console.log(err); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment