Skip to content

Instantly share code, notes, and snippets.

@fxg42
Last active February 26, 2016 21:41
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save fxg42/481bdf72440a94511c66 to your computer and use it in GitHub Desktop.
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
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()
}
})()
{
"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"
}
}
@fxg42
Copy link
Author

fxg42 commented Mar 19, 2015

requires iojs, exec with npm start

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment