Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Jan Küster 🔥
Jan Küster 🔥

Posted on • Edited on

     

Async Meteor Method calls

Meteor is mostly backward compatible to even < 1.0 projects. Therefore lots of code is still callback based. So is theMeteor.call method. You can easily wrap it into a Promise-based approach:

exportconstcallMethod=({name,args})=>newPromise((resolve,reject)=>{Meteor.call(name,args,(error,result)=>{if(error){returnreject(error)}returnresolve(result)})})
Enter fullscreen modeExit fullscreen mode

AlthoughMeteor.call supports multiple arguments I rather prefer to pass a single object with named arguments to keep the code more expressive.

You can then usecallMethod natively in an async environment:

Template.myTemplate.events({'click .some-button':asyncfunction(event,templateInstance){constage=awaitcallMethod({name:'getAge',args:{name:'John Doe'}})console.log(age)// whatever the method returned}})
Enter fullscreen modeExit fullscreen mode

Additionally you can "hook" into these calls and thus mix callbacks with promises and create a decent UX experience when method calls are part of user interactions:

exportconstcallMethod=({name,args,prepare,receive,success,failure})=>{// before callif(typeofprepare==='function'){prepare()}// create the promiseconstpromise=newPromise((resolve,reject)=>{Meteor.call(name,args,(error,result)=>{// on receivedif(typeofreceive==='function'){receive()}if(error){returnreject(error)}returnresolve(result)})})// on successif(typeofsuccess==='function'){promise.then(success)}// on errorif(typeoffailure==='function'){promise.catch(failure)}returnpromise}
Enter fullscreen modeExit fullscreen mode

The code can then be used for example to display a "waiting" indicator:

Template.myTemplate.events({'click .update-button':asyncfunction(event,templateInstance){constupdatedDoc=awaitcallMethod({name:'updateUser',args:{name:'John Doe',age:42},prepare:()=>templateInstance.state.set('updating',true),receive:()=>templateInstance.state.set('updating',false),failure:er=>alert(er),success:()=>alert('updated')})// process updatedDoc if desired})
Enter fullscreen modeExit fullscreen mode


I regularly publish articles here on dev.to aboutMeteor andJavaScript. If you like what you are reading and want to support me, you cansend me a tip via PayPal.

You can also find (and contact) me onGitHub,Twitter andLinkedIn.

Keep up with the latest development on Meteor by visitingtheir blog and if you are the same into Meteor like I am and want to show it to the world, you should check out theMeteor merch store.

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Graduated in Digital Media M.Sc. now developing the next generation of educational software. Since a while I develop full stack in Javascript using Meteor. Love fitness and Muay Thai after work.
  • Location
    Bremen, Germany
  • Education
    M.Sc.
  • Pronouns
    he/him
  • Work
    Scientific Employee at University of Bremen
  • Joined

More fromJan Küster 🔥

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp