title: Nodejs Creating a function that takes a function and executes it (creating a wrapper)
body: <p>my google foo is failing me with these keywords. </p>
  
  <p>Essentially I'm trying to eliminate typing the same code structure over and over again around functions that I am creating, so I thought to create a wrapper around the function I want to execute. This is messy and I can't seem to figure out how to make this "work" with what I'm doing. This is what I came up with so far, and its incorrect, but I pasted it here so you can have the basic idea. </p>
  
  <p>Im using promises and co-routine from bluebird.</p>
  
  <p>Wrapper:</p>
  
  <pre><code>'use strict';
  const Promise = require('bluebird');
  const {coroutine: co} = require('bluebird'); //Alias coroutine
  
  module.exports = function(func) {
      return new Promise((resolve, reject)=&gt;{
       co(function* () {
              try {
                  //Want to execute the function here
                  return func();
              } catch(e) {
                  reject(e);
              }
          })()
      });
  }
  </code></pre>
  
  <p>Function to feed in:</p>
  
  <pre><code>'use strict';
  const Promise = require('bluebird');
  const wrapper = require('./lib.js');
  let arg1 = 'arg1';
  
  wrapper(function(){
      //Print the argument then resolve
      console.log(arg1);
      resolve(arg1);
  }).then((data)=&gt;{
      console.log('done');
      console.log(data);
  })
  </code></pre>
  
  <p>Am I on the wrong track? I keep erroring out at the resolve or at the argument. 
  Essentially I just dont want to have to constantly declare the promise, co, and try/catch for each function I write. </p>