javascript - Using native ES6 promises with MongoDB -


i'm aware node driver mongo can promisified using external libraries. curious see if es6 promises used mongoclient.connect, tried (using babel 5.8.23 transpile):

import mongoclient 'mongodb';  function dbconnection({   host = 'localhost',   port = 27017,   database = 'foo' }) {   return new promise((resolve, reject) => {     mongoclient.connect(`mongodb://${host}:${port}/${database}`,      (err, db) => {       err ? reject(err) : resolve(db);     });   }); }  dbconnection({}).then(   db => {     let cursor = db.collection('bar').find();     console.log(cursor.count());   },   err => {     console.log(err);   } ); 

the output {promise <pending>}. cursors seems yield similar result. there way around or barking wrong tree entirely?

edit: node version 4.1.0.

there nothing around, expected behavior. cursor.count() returns promise, if want value, need use .then, e.g.

dbconnection({}).then(  db => {     let cursor = db.collection('bar').find();     return cursor.count();   } }).then(   count => {     console.log(count);   },   err => {     console.log(err);   } ); 

or simplified

dbconnection({}).then(db => db.collection('bar').find().count()).then(   count => console.log(count),   err => console.log(err) ); 

Comments