How to use BlueBird to promisify MeteorJS methods?


#1
 getACSxml : 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)     
        });
        req.end(function (res) {
        if (res.error) throw new Error(res.error);            
          console.log('getACSxml res.body is :', res.body);                        
        }); 

So that I can get returned value from getACSXml Meteor.method and chained to subsequent methods. It is something like?

var tmp = Promise.promisify(getACSXml);

tmp.then(function(result) {
console.log(result);
}


#2

Have you tried:


#3

Thanks Hugh, I was referring to how to Promisify my function, but it seems that you could promisify the actual Meteor.call

const callAsync = Promise.promisify(Meteor.call);
async function doSomething() {
    const res = await callAsync();
}

so no change needed in original getACSXml function, i,e return callback or return a promise?

I still need to promisify my own functions right, or is that being done for you by:

const callAsync = Promise.promisify(Meteor.call);?

#4

That will make it return a promise instead of a function with a callback


#5

Server.js:

import Promise from 'bluebird';

const callAsync = Promise.promisify(Meteor.call);
// whatever here

async function doSomething() {
    const res1 = await callAsync('getACSXml', '12345=', 'sec11');
    const res2 = await console.log('res1 outside is : ', res1);
}


Meteor.methods({ 

  getACSxml : 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)     
        });
        req.end(function (res) {
        if (res.error) throw new Error(res.error);            
          console.log('getACSxml res.body is :', res.body);                        
        });
});

Meteor.startup(() => {
     doSomething();

}); 

res1 is still undefined?