Bixby, Multiple HTTP calls in a single Javascript Function?

422 Views Asked by At

I know bixby developer studio is brand new, but I am having an issue making two http calls in one javascript function, first one to get a custom identifier from a service and then a second to get data from the service based on that identifier.

I have tried the following things:

module.exports.function = function(phoneNumber,couponBrand)
{
     if(phoneLookup(phoneNumber))
     {
        return getCoupons(couponBrand)
     }
     else
     {
       return null
     }
}

Which doesn't call either of the functions... So then I tried calling the first function as a precondition like so:

module.exports = {
  function:getCoupons,
  preconditions:[phoneLookup]
}

Which doesn't call the function, but only calls the precondition function... I also then tried doing a very nodeJS callback scheme where inside of the phoneLookup function I called the getCoupons function and passed a function as a parameter then at the end of the getCoupons function I invoke the parameter function as a callback while passing the values obtained in the phoneLookup function, like so:

function getCoupons(json,callback)
{
    var endpoint = //removed for security
    var body = //removed for brevity
    var options = //removed for brevity
    var response = http.postUrl(endpoint,body,options)
    var json = response.parsed
    callback(json)
}

module.exports.function = function phoneLookup(phoneNumber,couponBrand)
{
    var endpoint = //removed for security
    var body = //removed for brevity
    var options = //removed for brevity
    var response = http.postUrl(endpoint,body,options)
    var json = response.parsed

    getCoupons(json,function(results)
    {
        return results
    })
}

Sadly, this doesn't call the callback function or at least doesn't wait for the second http call in the getCoupons function to finish before returning to model I have listed in the output...

Anyone have any thoughts?

2

There are 2 best solutions below

0
On

I think, you can just simply call 2 functions sequentially.

like below,

function getCoupons(json,callback)
{
    var endpoint = //removed for security
    var body = //removed for brevity
    var options = //removed for brevity
    var response = http.postUrl(endpoint,body,options)
    var json = response.parsed
    return json
}

module.exports.function = function phoneLookup(phoneNumber,couponBrand)
{
    var endpoint = //removed for security
    var body = //removed for brevity
    var options = //removed for brevity
    var response = http.postUrl(endpoint,body,options)
    var json = response.parsed

    return getCoupons(json)
}

And, if you call http.xxxxUrl, your function waits for response. Bixby's javascript code runs sequentially. It does not support both async call and multi thread

2
On

Coding JavaScript functions in bixby is a little different, because everything runs synchronously. Avoid code that relies on promises or callbacks, because it may not work.

Here's a sample function from the docs, illustrating an HTTP GET. Try modifying it to use your code.

https://github.com/bixbydevelopers/http/blob/master/code/FindShoeFiltering.js

module.exports.function = function findShoe (type) {
  console.log("FindShoe filter by a specific type")
  var options = { 
    format: 'json',
    query: {
      type: type
    }
  };
  // If type is "Formal", then this makes a GET call to /shoes?type=Formal
  var response = http.getUrl(config.get('remote.url') + '/shoes', options);
  return response;
}