Skip to content

Instantly share code, notes, and snippets.

@zhaoyao91
Created May 25, 2017 11:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zhaoyao91/14aa4bf44f756f0cfaeee6d491bd87d1 to your computer and use it in GitHub Desktop.
Save zhaoyao91/14aa4bf44f756f0cfaeee6d491bd87d1 to your computer and use it in GitHub Desktop.
pomisify meteor apis
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