Created
May 25, 2017 11:25
-
-
Save zhaoyao91/14aa4bf44f756f0cfaeee6d491bd87d1 to your computer and use it in GitHub Desktop.
pomisify meteor apis
This file contains hidden or 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
import { Meteor } from 'meteor/meteor' | |
import { Accounts } from 'meteor/accounts-base' | |
import { assoc, keys, isFunction, reduce } from 'lodash/fp' | |
export default function () { | |
promisifyObject(Meteor) | |
promisifyObject(Accounts) | |
} | |
function promisifyObject (target) { | |
target.async = reduce((asyncMethods, key) => { | |
return { | |
...asyncMethods, | |
[key](...args) { | |
return new Promise((resolve, reject) => { | |
target[key](...args, (err, result) => { | |
if (err) reject(err) | |
else resolve(result) | |
}) | |
}) | |
} | |
} | |
}, {}, keys(target).filter((key) => isFunction(target[key]))) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment