Start using async/await instead of promises and callbacks

If one of the promises I am awaiting throws an error, what will happen?

You can use try/catch!

try {
  await promise;
} catch (e) {
  ...
}
4 Likes

For what its worth, I find asyn/await the most natural pattern in dealing with async code and try/catch is already a uniform way of error handling.

5 Likes

Good stuff. This seems like the ideal way to write apps. Does anyone know of a good Meteor 1.3 React example app using async/await?

2 Likes

Not sure, but my understanding is that that error gets returned, not swallowed, but I think it’s up to you how you handle it.

Why are there not that many actual MeteorJS server side examples? Tried to use async and await and was wondering why results are not ordered as expected when I clicked on a button?

In my case the client/main.js:

Template.resultfromACS.events({
'click .get_id' (event, instance) {
      (async function () {
          var result1 = await Meteor.callPromise('getXml', '123456789=', 'seb'); 
          var result2 = await Meteor.callPromise('testFunction', result1);
       }());
   } 
});

in server/main.js:

Meteor.methods({

    getXml : async function (authorizationCode, username) {
        var url = "".concat("https://myHost/Rest/Identity/User/name/",username);
        var req = unirest("GET", url);
        req.headers({
        "content-type": "application/xml",
        "authorization": "".concat("Basic " ,authorizationCode)
        });
        await req.end(function (res) {
            if (res.error) throw new Error(res.error);         
            console.log('res.body is :', res.body);  
        });  
      }, 

      testFunction: async function (message) {
            console.log('Message is : ', message);
      }
});

Results in:

Message is : null
res.body is :<?xml version=“1.0” encoding=“UTF-8” standalone=“yes”?

Why is the second call to testFunction happening before the first call to getXml in this case, as I need to get result from first call and pass result1 into second method?


Would this be something better to use than async/await?

You can use them together with the promise mixin: https://atmospherejs.com/didericis/callpromise-mixin

So you could do:

await myMethod.callPromise(args);

And there’s a variety of other mixins as well! https://github.com/meteor/validated-method#community-mixins

thanks @sashko but how do you do it on the client? I’m getting stuck with this.

Async/Await is not supported on the meteor client, what can we use? Should I wrapasync the method?

I thought it was in Meteor 1.3, no?

1 Like

For people who are using Angular2-Metoer, I recommend using the built-in RxJS 5 (Observable) which is much more powerful!

Also, I posted it here about how to Subscribe data in Service using RxJS 5 which gives you a feel to start using the more powerful RxJS 5.

Hi, tried async with Meteor 1.3.3.1.
async function(...argus)=>{ // console.log("call"+id); return callMeteorMethod(name,...argus); }
Got an error:
Unexpected token (49:38)

What am I missing here? Any extra package to support this?

Thanks!

You’re using the arrow function syntax incorrectly by also typing the function keyword.

My Bad! Still not familiar with the new js. Thanks! It works!

1 Like

How I can manage the following?

  1. When Meteor.call for validateObject is triggered
  2. Waterfall goes thru the callbacks
  3. At last the final result of the waterfall is returned to the Meteor.call via Meteor.method function and printed into console.log

This would be super useful :smile_cat:

var async = require("async");
var waterfall = require("async/waterfall");

  Meteor.methods({
    'validateObject': function(object) {
      async.waterfall([
        function(callback)  {
          // run first 
          callback(null, result);
        },
        function(result, callback)  {
          // run second
          callback(null, result);
        }
      ], function(error, result) {
          // at the end provide return to Method 'validateObject';
      });
    }
  });

  var object = {_id: "string", name: "string"}

  Meteor.call("validateObject", object, function(error, result){
    if(result) {
      // Display the return from Method 'validateObject';
      console.log(result);
    }
  });

Hello @trusktr I am little confused about using async/await in meteor method in server side
I am trying this code, but doesn’t works:

const requestWithPromise = async (options) => {
  // Axios create a promise to return request result: https://www.npmjs.com/package/axios
  const result = await axios(options); 
  console.log('showing axios result', result.data);
  return result.data;
};

Meteor.methods({
  'Request.execute'(data) {
    const options = {method: 'post', url, data, timeout};
    const result = requestWithPromise(options);
    console.log('showing myPromise result' , JSON.stringify(result));
    return result;
  }
});

In my meteor method I should wait for request result, but the log order is the following:

showing myPromise result {}
showing  axios result {id:.............}

You’re missing the await:

const result = await requestWithPromise(options);

Hi @sashko, I can’t use await inside a meteor method, because I should define the method as async and it generates error, but I fixed my problem with a simple thing:

  'Request.execute'(data) {
    const options = {method: 'post', url, data, timeout};
    const response = Promise.await(axios(options));
    console.log('SHOWING ASYNC' , JSON.stringify(response.data));
    return response.data;
  }

I am working with Meteor@1.4.2.
This is the required package https://github.com/meteor/promise

1 Like

@sashko Meteor supports async methods now?

1 Like