Methods + ajax await-async or promise or future or?


#1

Using Meteor 1.5. Need help understanding what and how I should work with async calls.

Client calls Method:
Meteor.call(‘getBalances’, (error, result) => {
console.log(result, ‘res’);
console.log(error, ‘error’);
});

Method init class and call getBalance function:
getBalances() {
this.bittrex = new Bittrex(secretkey, apikey);
this.bittrex.getBalances();
console.log(balances, ‘balances’);
return balances;
}

getBalance function uses node api to get balance from external site bittrex.
getBalances () {
bittrex.getbalances((data) => {
console.log(data, ‘data’);
return data;
});
}

The callback sent to bittrex consoles out perfectly fine. But how do I make the meteor method function wait for response? Whats the optimal way to do it and some simple example code would be appreciated.


#2

You can try Meteor.wrapAsync to get meteor-friendly asyncronous function like:

const unwrappedFunction = (foo, bar, callback) => {
  /*totally async code*/
  callback(error, data)
}
const wrappedFunction = Meteor.wrapAsync(unwrappedFunction)