Skip to main content
Commonmark migration
Source Link

After going through the "co" GitHub code, I was able to remove the extra boilerplate code. I made following changes to improve my code:

  1. Used the "co" promise and return it in my async function. This help to remove the extra "Promise" wrapper code.
  2. To resolve the "co" promise, I returned the "Promise" as per fails/success scenario.
  3. When making call to MongoDB native driver, used ".catch" to catch the error there itself and apply logic.

Following is my final version which is neater and cleaner using the "co" module:

#crud.js

crud.js

'use strict';
const db = require("./dbmanager").db;
const co = require("co");

module.exports = {

  getAllDocuments: (collectionName)=> {
      //return the "co" promise instead of using "Promise" boilerplate code
      co(function*() {
        let docs = yield db.collection(collectionName).find().toArray().catch(function(err) {
        //in case of error, return the custom message and log the error returned by mongodb driver
        console.log('Error occurred to get documents', err);
        return Promise.reject(new Error(`Error occurred to get documents`)); //return the Promise which will be used by "co"
      });
        return Promise.resolve(docs); //return the Promise which will be used by "co"

      });  
  }
}; 

book.js

'use strict';
const dbcrud   = require("./crud");
const co = require("co");
module.exports = {

  getTransactions: (req, res) => {
   //use "co" to make asyn function call without callback
    co(function*() {
      let transactions = yield dbcrud.getTransactions('transactions');
      res.json(transactions);
    }).catch((err) => {
      console.error(`[ERROR:Controller:API:getTransactions]: error`, err);
      res.status(500);
      res.json({error: err.message});
    });
  },

};

After going through the "co" GitHub code, I was able to remove the extra boilerplate code. I made following changes to improve my code:

  1. Used the "co" promise and return it in my async function. This help to remove the extra "Promise" wrapper code.
  2. To resolve the "co" promise, I returned the "Promise" as per fails/success scenario.
  3. When making call to MongoDB native driver, used ".catch" to catch the error there itself and apply logic.

Following is my final version which is neater and cleaner using the "co" module:

#crud.js

'use strict';
const db = require("./dbmanager").db;
const co = require("co");

module.exports = {

  getAllDocuments: (collectionName)=> {
      //return the "co" promise instead of using "Promise" boilerplate code
      co(function*() {
        let docs = yield db.collection(collectionName).find().toArray().catch(function(err) {
        //in case of error, return the custom message and log the error returned by mongodb driver
        console.log('Error occurred to get documents', err);
        return Promise.reject(new Error(`Error occurred to get documents`)); //return the Promise which will be used by "co"
      });
        return Promise.resolve(docs); //return the Promise which will be used by "co"

      });  
  }
}; 

book.js

'use strict';
const dbcrud   = require("./crud");
const co = require("co");
module.exports = {

  getTransactions: (req, res) => {
   //use "co" to make asyn function call without callback
    co(function*() {
      let transactions = yield dbcrud.getTransactions('transactions');
      res.json(transactions);
    }).catch((err) => {
      console.error(`[ERROR:Controller:API:getTransactions]: error`, err);
      res.status(500);
      res.json({error: err.message});
    });
  },

};

After going through the "co" GitHub code, I was able to remove the extra boilerplate code. I made following changes to improve my code:

  1. Used the "co" promise and return it in my async function. This help to remove the extra "Promise" wrapper code.
  2. To resolve the "co" promise, I returned the "Promise" as per fails/success scenario.
  3. When making call to MongoDB native driver, used ".catch" to catch the error there itself and apply logic.

Following is my final version which is neater and cleaner using the "co" module:

crud.js

'use strict';
const db = require("./dbmanager").db;
const co = require("co");

module.exports = {

  getAllDocuments: (collectionName)=> {
      //return the "co" promise instead of using "Promise" boilerplate code
      co(function*() {
        let docs = yield db.collection(collectionName).find().toArray().catch(function(err) {
        //in case of error, return the custom message and log the error returned by mongodb driver
        console.log('Error occurred to get documents', err);
        return Promise.reject(new Error(`Error occurred to get documents`)); //return the Promise which will be used by "co"
      });
        return Promise.resolve(docs); //return the Promise which will be used by "co"

      });  
  }
}; 

book.js

'use strict';
const dbcrud   = require("./crud");
const co = require("co");
module.exports = {

  getTransactions: (req, res) => {
   //use "co" to make asyn function call without callback
    co(function*() {
      let transactions = yield dbcrud.getTransactions('transactions');
      res.json(transactions);
    }).catch((err) => {
      console.error(`[ERROR:Controller:API:getTransactions]: error`, err);
      res.status(500);
      res.json({error: err.message});
    });
  },

};
separated filenames from code
Source Link
Edward
  • 66.6k
  • 4
  • 118
  • 282

After going through the "co" GitHub code, I was able to remove the extra boilerplate code. I made following changes to improve my code:

  1. Used the "co" promise and return it in my async function. This help to remove the extra "Promise" wrapper code.
  2. To resolve the "co" promise, I returned the "Promise" as per fails/success scenario.
  3. When making call to MongoDB native driver, used ".catch" to catch the error there itself and apply logic.

Following is my final version which is neater and cleaner using the "co" module:

#crud.js

crud.js
'use strict';
const db = require("./dbmanager").db;
const co = require("co");

module.exports = {

  getAllDocuments: (collectionName)=> {
      //return the "co" promise instead of using "Promise" boilerplate code
      co(function*() {
        let docs = yield db.collection(collectionName).find().toArray().catch(function(err) {
        //in case of error, return the custom message and log the error returned by mongodb driver
        console.log('Error occurred to get documents', err);
        return Promise.reject(new Error(`Error occurred to get documents`)); //return the Promise which will be used by "co"
      });
        return Promise.resolve(docs); //return the Promise which will be used by "co"

      });  
  }
}; 

book.js

book.js

'use strict';
const dbcrud   = require("./crud");
const co = require("co");
module.exports = {

  getTransactions: (req, res) => {
   //use "co" to make asyn function call without callback
    co(function*() {
      let transactions = yield dbcrud.getTransactions('transactions');
      res.json(transactions);
    }).catch((err) => {
      console.error(`[ERROR:Controller:API:getTransactions]: error`, err);
      res.status(500);
      res.json({error: err.message});
    });
  },

};

After going through the "co" GitHub code, I was able to remove the extra boilerplate code. I made following changes to improve my code:

  1. Used the "co" promise and return it in my async function. This help to remove the extra "Promise" wrapper code.
  2. To resolve the "co" promise, I returned the "Promise" as per fails/success scenario.
  3. When making call to MongoDB native driver, used ".catch" to catch the error there itself and apply logic.

Following is my final version which is neater and cleaner using the "co" module:

crud.js
'use strict';
const db = require("./dbmanager").db;
const co = require("co");

module.exports = {

  getAllDocuments: (collectionName)=> {
      //return the "co" promise instead of using "Promise" boilerplate code
      co(function*() {
        let docs = yield db.collection(collectionName).find().toArray().catch(function(err) {
        //in case of error, return the custom message and log the error returned by mongodb driver
        console.log('Error occurred to get documents', err);
        return Promise.reject(new Error(`Error occurred to get documents`)); //return the Promise which will be used by "co"
      });
        return Promise.resolve(docs); //return the Promise which will be used by "co"

      });  
  }
}; 

book.js
'use strict';
const dbcrud   = require("./crud");
const co = require("co");
module.exports = {

  getTransactions: (req, res) => {
   //use "co" to make asyn function call without callback
    co(function*() {
      let transactions = yield dbcrud.getTransactions('transactions');
      res.json(transactions);
    }).catch((err) => {
      console.error(`[ERROR:Controller:API:getTransactions]: error`, err);
      res.status(500);
      res.json({error: err.message});
    });
  },

};

After going through the "co" GitHub code, I was able to remove the extra boilerplate code. I made following changes to improve my code:

  1. Used the "co" promise and return it in my async function. This help to remove the extra "Promise" wrapper code.
  2. To resolve the "co" promise, I returned the "Promise" as per fails/success scenario.
  3. When making call to MongoDB native driver, used ".catch" to catch the error there itself and apply logic.

Following is my final version which is neater and cleaner using the "co" module:

#crud.js

'use strict';
const db = require("./dbmanager").db;
const co = require("co");

module.exports = {

  getAllDocuments: (collectionName)=> {
      //return the "co" promise instead of using "Promise" boilerplate code
      co(function*() {
        let docs = yield db.collection(collectionName).find().toArray().catch(function(err) {
        //in case of error, return the custom message and log the error returned by mongodb driver
        console.log('Error occurred to get documents', err);
        return Promise.reject(new Error(`Error occurred to get documents`)); //return the Promise which will be used by "co"
      });
        return Promise.resolve(docs); //return the Promise which will be used by "co"

      });  
  }
}; 

book.js

'use strict';
const dbcrud   = require("./crud");
const co = require("co");
module.exports = {

  getTransactions: (req, res) => {
   //use "co" to make asyn function call without callback
    co(function*() {
      let transactions = yield dbcrud.getTransactions('transactions');
      res.json(transactions);
    }).catch((err) => {
      console.error(`[ERROR:Controller:API:getTransactions]: error`, err);
      res.status(500);
      res.json({error: err.message});
    });
  },

};
Notice removed Insufficient justification by Jamal
added 3 characters in body
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

After going through the "co" github"co" GitHub code, I was able to remove the extra boilerplate code. I made following changes to improve my code.:

  1. Used the "co""co" promise and return it in my async function. This help to remove the extra "Promise" wrapper code.
  2. To resolve the "co" promise, I returned the "Promise" as per fails/success scenario.
  3. When making call to mongodbMongoDB native driver, used ".catch"".catch" to catch the error there itself and apply logic.

Following is my final version of code which is more neatneater and cleancleaner using "co"the "co" module.:

crud.js
'use strict';
const db = require("./dbmanager").db;
const co = require("co");

module.exports = {

  getAllDocuments: (collectionName)=> {
      //return the "co" promise instead of using "Promise" boilerplate code
      co(function*() {
        let docs = yield db.collection(collectionName).find().toArray().catch(function(err) {
        //in case of error, return the custom message and log the error returned by mongodb driver
        console.log('Error occurred to get documents', err);
        return Promise.reject(new Error(`Error occurred to get documents`)); //return the Promise which will be used by "co"
      });
        return Promise.resolve(docs); //return the Promise which will be used by "co"

      });  
  }
}; 

book.js
'use strict';
const dbcrud   = require("./crud");
const co = require("co");
module.exports = {

  getTransactions: (req, res) => {
   //use "co" to make asyn function call without callback
    co(function*() {
      let transactions = yield dbcrud.getTransactions('transactions');
      res.json(transactions);
    }).catch((err) => {
      console.error(`[ERROR:Controller:API:getTransactions]: error`, err);
      res.status(500);
      res.json({error: err.message});
    });
  },

};

After going through the "co" github code, I was able to remove the extra boilerplate code. I made following changes to improve my code.

  1. Used the "co" promise and return it in my async function. This help to remove the extra "Promise" wrapper code
  2. To resolve the "co" promise, I returned the "Promise" as per fails/success scenario
  3. When making call to mongodb native driver, used ".catch" to catch the error there itself and apply logic.

Following is my final version of code which is more neat and clean using "co" module.

crud.js
'use strict';
const db = require("./dbmanager").db;
const co = require("co");

module.exports = {

  getAllDocuments: (collectionName)=> {
      //return the "co" promise instead of using "Promise" boilerplate code
      co(function*() {
        let docs = yield db.collection(collectionName).find().toArray().catch(function(err) {
        //in case of error, return the custom message and log the error returned by mongodb driver
        console.log('Error occurred to get documents', err);
        return Promise.reject(new Error(`Error occurred to get documents`)); //return the Promise which will be used by "co"
      });
        return Promise.resolve(docs); //return the Promise which will be used by "co"

      });  
  }
}; 

book.js
'use strict';
const dbcrud   = require("./crud");
const co = require("co");
module.exports = {

  getTransactions: (req, res) => {
   //use "co" to make asyn function call without callback
    co(function*() {
      let transactions = yield dbcrud.getTransactions('transactions');
      res.json(transactions);
    }).catch((err) => {
      console.error(`[ERROR:Controller:API:getTransactions]: error`, err);
      res.status(500);
      res.json({error: err.message});
    });
  },

};

After going through the "co" GitHub code, I was able to remove the extra boilerplate code. I made following changes to improve my code:

  1. Used the "co" promise and return it in my async function. This help to remove the extra "Promise" wrapper code.
  2. To resolve the "co" promise, I returned the "Promise" as per fails/success scenario.
  3. When making call to MongoDB native driver, used ".catch" to catch the error there itself and apply logic.

Following is my final version which is neater and cleaner using the "co" module:

crud.js
'use strict';
const db = require("./dbmanager").db;
const co = require("co");

module.exports = {

  getAllDocuments: (collectionName)=> {
      //return the "co" promise instead of using "Promise" boilerplate code
      co(function*() {
        let docs = yield db.collection(collectionName).find().toArray().catch(function(err) {
        //in case of error, return the custom message and log the error returned by mongodb driver
        console.log('Error occurred to get documents', err);
        return Promise.reject(new Error(`Error occurred to get documents`)); //return the Promise which will be used by "co"
      });
        return Promise.resolve(docs); //return the Promise which will be used by "co"

      });  
  }
}; 

book.js
'use strict';
const dbcrud   = require("./crud");
const co = require("co");
module.exports = {

  getTransactions: (req, res) => {
   //use "co" to make asyn function call without callback
    co(function*() {
      let transactions = yield dbcrud.getTransactions('transactions');
      res.json(transactions);
    }).catch((err) => {
      console.error(`[ERROR:Controller:API:getTransactions]: error`, err);
      res.status(500);
      res.json({error: err.message});
    });
  },

};
updated details on change
Source Link
joy
  • 151
  • 7
Loading
Notice added Insufficient justification by 200_success
Source Link
joy
  • 151
  • 7
Loading