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?
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
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);
}
});
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:
Yes. It works exactly as it should when called from the client (the Promise is resolved before the result is sent back over the wire).
When I was playing with this a few months back I got unexpected results (the Promise itself) when calling an async method from within the same server code (not something I normally do).
Hm, so given a methods-calling-methods situation, eventually some of those methods will be getting called form the server as well. And then it might (based on what I understand from your experience) cause issues, right?
Perhaps. I found that behaviour, but I haven’t revisited those tests for quite a while, so it may be fixed now. I should probably take another look. I was putting a repro together for @benjamn, but got sidetracked and didn’t finish it.