vasansr / pro-mern-stack

Code Listing for the book Pro MERN Stack

Home Page:http://www.apress.com/in/book/9781484226520

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Async Await Paradigm

KhalilMohammad opened this issue · comments

In ch-6 ,You explained 4 different paradigm for dealing with MongoDb.
I think Es7 async await pattern is the most simple approach.
Babel also provides Es2017 preset.
I think you should include this paradigm in the book and make it default .

const testWithAsyncAwait = async () => {
let db = await MongoClient.connect('mongodb://localhost/playground');
try {
const result = await db.collection('employees').insertOne({ id: 1, name: 'C. Generator' });
console.log('Result of insert:', result.insertedId);
const docs = await db.collection('employees').find({ id: 1 }).toArray();
console.log('Result of find:', docs);
db.close();
} catch (err) {
console.log('ERROR', err);
}
}

It is similar to generator paradigm