Skip to content

Instantly share code, notes, and snippets.

@tehsis
Created August 8, 2016 15:59
Show Gist options
  • Save tehsis/cb5906a11595e0bada72272a4ef2203e to your computer and use it in GitHub Desktop.
Save tehsis/cb5906a11595e0bada72272a4ef2203e to your computer and use it in GitHub Desktop.
co/q
const co = require('q').spawn;
// const co = require('co');
function wait (ms, value) {
return new Promise((resolve, reject) => {
if (typeof ms !== 'number') {
return reject(new Error('arg must be a number'));
}
setTimeout(() => {
resolve(value);
}, ms);
});
}
co(function* () {
let x = wait(3000, 3);
let y = wait(3000, 5);
let z = wait('foo', 4);
let xv,xy;
try {
xv = yield x;
yv = yield y;
} catch (e) {
console.log('Error unexpected', e);
}
try {
xz = yield z;
} catch (e) {
console.log('Error expected', e);
}
console.log(xv + yv);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment