Created
August 8, 2016 15:59
-
-
Save tehsis/cb5906a11595e0bada72272a4ef2203e to your computer and use it in GitHub Desktop.
co/q
This file contains 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
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