Synchronous or Asynchronous call in this situation


#1

Normally all RESTful calls should be asynchronus for non-blocking, but I would like to first make a GET call so that for any given user I can get its corresponding id which will be needed for subsequent PUT call to modify another user attribute

<description>Test Description</description>
<id>123</id>
<identityGroupName>All Groups:Telecomms Design</identityGroupName>
<name>seb11</name>

My question is it better to make GET to be synchronous so that the second PUT call won’t start until the first GET is completed since it is dependent on its return value? Or can I use Promise or other ways to make everything all asynchronous?


#2

Go with asynchronous and promises or async/await. Both support chaining so you can wait for one request to complete before starting the next. If you’re interested in using async/await with Meteor 1.3, check out:


#3

Thanks hwillson, but 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?

and how does the deanius:promise differs to the Meteor._wrapAsync

or is this because I am not using ES7 within Pycharm with the MeteorJS plugin?

or related to not being server side call?