Skip to content

Instantly share code, notes, and snippets.

@laterbreh
Last active March 24, 2017 19:25
Show Gist options
  • Save laterbreh/17492779a499ef58ea950bc71233491f to your computer and use it in GitHub Desktop.
Save laterbreh/17492779a499ef58ea950bc71233491f to your computer and use it in GitHub Desktop.
Creating a wrapper for promise based co-routine functions
//This is the DB file im writing the fucntions in
const redis = Promise.promisifyAll(require('redis'));
const client = redis.createClient();
const Promise = require('bluebird');
const {coroutine: co} = require('bluebird'); //Alias coroutine
const wrapper = require('./lib.js');
module.exports.SomeFunction(key, value) {
return wrapper(function(){
let set = yield client.set(key, value);
return Promise.resolve(set);
})
}
//Lib
'use strict';
const Promise = require('bluebird');
const {coroutine: co} = require('bluebird'); //Alias coroutine
module.exports = function(func) {
return new Promise((resolve, reject)=>{
co(function* () {
try {
//Want to execute the function here
func().then(result => {
resolve(result);
}).catch(e => {
reject(e)
})
} catch(e) {
reject(e);
}
})()
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment