Created
November 21, 2016 00:47
-
-
Save rahatarmanahmed/a6a0e4640672cbfc9381230291262e90 to your computer and use it in GitHub Desktop.
A promise plugin for choo, which I made before I found out that barracks-promisify-plugin already existed.
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
var isPromise = require('is-promise') | |
var pify = require('pify') | |
module.exports = function (opts) { | |
opts = opts || {} | |
function wrapEffects (fn) { | |
return function (data, state, send, done) { | |
send = pify(send, opts.Promise) | |
var promise = fn(data, state, send, done) | |
if (isPromise(promise)) promise.then(function (res) { done(null, res) }, done) | |
} | |
} | |
function wrapSubscriptions (fn) { | |
return function (send, done) { | |
send = pify(send, opts.Promise) | |
var promise = fn(send, done) | |
if (isPromise(promise)) promise.then(function (res) { done(null, res) }, done) | |
} | |
} | |
return { | |
wrapEffects: wrapEffects, | |
wrapSubscriptions: wrapSubscriptions | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment