Skip to content

Instantly share code, notes, and snippets.

@christkv
Created March 4, 2016 18:21
Show Gist options
  • Save christkv/a5d44adf9c5405f939d0 to your computer and use it in GitHub Desktop.
Save christkv/a5d44adf9c5405f939d0 to your computer and use it in GitHub Desktop.
es6 promises and generators
var a = new Promise(function(reject, resolve) {
if(err) return reject(err);
.......
resolve(value);
});
var b = new Promise(function(reject, resolve) {
if(err) return reject(err);
.......
resolve(value);
});
results = yield Promise.all([a,b])
a.then(function(value) {
}).catch(function(err) {
});
var co = require('co');
co(function*() {
var db = yield MongoClient.connect('....');
var value = yield db.collection('a').findOne()
}).catch(function(e) {
});
User.prototype.getUser = function() {
var self = this;
return new Promise(function(resolve, reject) {
co(function*() {
var user = yield self.db.collection('users').findOne();
resolve(user);
}).catch(reject);
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment