API Call in Code Function - How to use resolve outside my function?

(Sebastian Weiß) #1

Hi all,

I would like to write a function which returns the price of an Item. I get this information from a webservice. I can’t use HTTP Request service as I require 'sendImmediately': false.

So the following code returns me the price. But only for one Item:

function getPriceOfItem(articleNumber) {


    var user = 'apiuser';
    var pass = 'apikey';

    var opts = {
        method: 'GET',
        url: 'http://example.com/api/articles/' + articleNumber + '?useNumberAsId=true',
        json: true,
        auth: {
            'user': user,
            'pass': pass,
            'sendImmediately': false
        },

    };

    var price;


    utils.request(opts, function (err, resp, body) {
        if (err) {
            return reject(err);
        }



        price = body.data.mainDetail.prices[0].price

        //This returns the price. But I would like to give it back to the function as an return
         resolve(price)



    });



}



getPriceOfItem('SW10002')

So this is not working:

function getPriceOfItem(articleNumber) {

    
    var user = 'apiuser';
    var pass = 'apikey';

    var opts = {
        method: 'GET',
        url: 'http://example.com/api/articles/' + articleNumber + '?useNumberAsId=true',
        json: true,
        auth: {
            'user': user,
            'pass': pass,
            'sendImmediately': false
        },

    };

    var price;
   

    utils.request(opts, function (err, resp, body) {
        if (err) {
            return reject(err);
        }


       
       price = body.data.mainDetail.prices[0].price
           


 



    });

    return price
    
    

    


}


//This should return the sum of the two items. But it actually returns nothing. 
resolve(getPriceOfItem('SW10002') + getPriceOfItem('SW10003'))

So the question is: How can I get the price with the function? I guess I need to work with some callbacks. But I don’t get it

Regards
Sebastian

(John Jackson) #2

Hi Sebastian,

You’ve hit one of the downsides to async code! Because the utils.request is ‘asyncronous’, it returns price immediately, way before the actual result comes back from the API.

You can chain the callbacks, like this:

var user = 'apiuser';
var pass = 'apikey';

var opts = {
    method: 'GET',
    url: 'http://example.com/api/articles/SW10002?useNumberAsId=true',
    json: true,
    auth: {
        'user': user,
        'pass': pass,
        'sendImmediately': false
    }
}

utils.request(opts, function (err, resp, body) {
    if (err) {
        return reject(err);
    }

    var price = body.data.mainDetail.prices[0].price

    var opts = {
        method: 'GET',
        url: 'http://example.com/api/articles/SW20003?useNumberAsId=true',
        json: true,
        auth: {
            'user': user,
            'pass': pass,
            'sendImmediately': false
        }
    }

    utils.request(opts, function (err, resp, body) {
        if (err) {
            return reject(err);
        }

        var price2 = body.data.mainDetail.prices[0].price

        resolve(price + price2)

    });

});

As long as you know how many prices you need to get (in the above case it’s 2), then this will work. Of course, you need to substitute in the ID’s as they’re hardcoded in this example.

Also I haven’t tested the code so hopefully you can fix any errors! :slight_smile:

1 Like
(Sebastian Weiß) #3

Oh yes. This will work. But I require the function as will later use an array to go through.

I have currently switched to one of your competitors. As they provide me a better console for debugging (Promises, console.log(), and it shows me the line number where the error happens, and I don’t need to trigger my code from the chat-bot. I can test it directly within the console which makes the developing workflow much faster)

But they don’t offer so much plugins

Do you plan some extended futures for the new GUI?