Last active
March 24, 2017 19:25
-
-
Save laterbreh/17492779a499ef58ea950bc71233491f to your computer and use it in GitHub Desktop.
Creating a wrapper for promise based co-routine functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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); | |
}) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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