Super simple modernization of method calls, with a tiny implementation. I basically wanted a tiny function I could call which would return a Promise for Meteor method calls, so I can do async/await. This is that tiny function:
// This may require use of meteor/promise to work on server side with fibers - not sure
export const callAsync = (methodName, ...args) => new Promise(
(resolve, reject) => Meteor.call(methodName, ...args, (error, result) => {
if (error) reject(error)
resolve(result)
})
)
But then I thought, it’s still weird to call a method with a string, like await callAsync("methodName", "arg1", "arg2") so I remembered that Proxy objects are a thing (I used to use those in ES4/AS3 for RPC fun!).
So I made a quick Proxy wrapper:
export const Methods = new Proxy({}, {
get: (obj, key) => (...args) => callAsync(key, ...args)
})
So now I can import {Methods} from '/imports/utils/methods' and then call methods as though they are methods!
await Methods.methodName("arg1", "arg2")
This may even be a nicer way to expose new async method calls than reworking Meteor.call - which I always found weird anyway due to its clash with Function.call.
Once we add in polyfills and babel transform overhead, this gets larger, but with Meteor’s upcoming duel build system, this will remain tiny in the bundle. And that’s awesome!