Last active
February 26, 2016 21:41
-
-
Save fxg42/481bdf72440a94511c66 to your computer and use it in GitHub Desktop.
Using Bluebird Promises, MongoDB and Babel's async/await with Bluebird's coroutines
This file contains hidden or 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
import Promise from 'bluebird' | |
import MongoDB from 'mongodb' | |
Promise.promisifyAll(MongoDB) | |
async function findEveryone(db) { | |
const people = db.collection('people') | |
const everyone = await people.find().toArrayAsync() | |
return everyone.map( x => x.name ) | |
} | |
(async function run() { | |
try { | |
var db = await MongoDB.MongoClient.connectAsync('mongodb://localhost/test') | |
const everyone = await findEveryone(db) | |
console.log(everyone) | |
} catch (e) { | |
console.log('Something went wrong', e) | |
} finally { | |
db && db.close() | |
} | |
})() |
This file contains hidden or 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
{ | |
"name": "bluebird-async-await", | |
"version": "1.0.0", | |
"description": "", | |
"main": "index.js", | |
"scripts": { | |
"start": "./node_modules/.bin/babel-node --experimental --optional bluebirdCoroutines index.js" | |
}, | |
"author": "", | |
"license": "WTFPL", | |
"dependencies": { | |
"babel": "^4.7.16", | |
"bluebird": "^2.9.14", | |
"mongodb": "^1.4.34" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
requires iojs, exec with
npm start