Created
May 17, 2017 15:42
-
-
Save 7flash/10edf938a986c3f5d8b37c6194e5d387 to your computer and use it in GitHub Desktop.
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
var co = require('co'); | |
function getResult(item) { | |
return new Promise((resolve, reject) => { | |
setTimeout(() => { | |
if(item > 5) { | |
reject('Break! Last page reached.'); | |
} else { | |
resolve(item * 2); | |
} | |
}, 100); | |
}); | |
} | |
function* sequence() { | |
let i = 1; | |
let arr = []; | |
try { | |
while(i++) { | |
arr.push(yield getResult(i)); | |
} | |
} catch(e) { | |
console.log(e); | |
} | |
return arr; | |
} | |
co(function* () { | |
let result = yield sequence(); | |
console.log(result); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment