0

I'm writing a Javascript function that will return a number as result. This is my code:

get_tiquete_hacienda: function (){
var myconsecutivo = 0;

            var rpc2 = require('web.rpc');
            rpc2.query({
                model: 'pos.order',
                method: 'compute_sales_bsi'
            }).then(function(res) {
                myconsecutivo = res;
                console.log('soy el otro: ' + myconsecutivo);
             });
return myconsecutivo;
}

If i look at the console, "soy el otro:" renders the correct value but return myconsecutivo is undefined

Why is this the case?

chevybow
  • 9,959
  • 6
  • 24
  • 39
Jason
  • 85
  • 1
  • 1
  • 7

2 Answers2

1
get_tiquete_hacienda: function (){
var myconsecutivo = 0;

            var rpc2 = require('web.rpc');
            return rpc2.query({
                model: 'pos.order',
                method: 'compute_sales_bsi'
            }).then(function(res) {
                myconsecutivo = res;
                console.log('soy el otro: ' + myconsecutivo);
                return myconsecutivo;
             });
}

get_tiquete_hacienda().then(function (myconsecutivo) {
  // myconsecutivo is correct here
});

Or more succinctly:

get_tiquete_hacienda: function (){  
  var rpc2 = require('web.rpc');
  return rpc2.query({
    model: 'pos.order',
    method: 'compute_sales_bsi'
  }).then(function(res) {
    console.log('soy el otro: ' + res);
    return res;
  });
}

get_tiquete_hacienda().then(function (myconsecutivo) {
  // myconsecutivo is correct here
});
Will
  • 6,601
  • 3
  • 31
  • 42
0

You need to return the value inside the “then” statement.

LawfulGood
  • 177
  • 5