Generators / yield (es2015) 和 Async / await(es7)
function* foo () {
var index = 0;
while (index < 2) {
yield index++;
}
}
var bar = foo();
console.log(bar.next()); // { value: 0, done: false }
console.log(bar.next()); // { value: 1, done: false }
console.log(bar.next()); // { value: undefined, done: true }
co(function* (){
yield Something.save();
}).then(function() {
// success
})
.catch(function(err) {
//error handling
});
async function save(Something) {
try {
await Something.save()
} catch (ex) {
//error handling
}
console.log('success');
}