0

The title is little bit confusing because I am not sure how to explain it. So there is the code:

let label = getLabels(min, max)
console.log(label)

and getLabels:

function getLabels(minId, maxId){
    $.ajax({
        method: 'post',
        url: 'ajaxs/get-attribute-value-name',
        data: {
            min: minId,
            max: maxId
        },
        success: function (data) {
            return {
                min: data.min,
                max: data.max
            }
        }
    })
}

What I get in the console is undefined. What is the proper way to assign obejct to label variable? Is it good practice at all? Both data.min and data,max return what is expected. Thank you!

Toma Tomov
  • 1,476
  • 19
  • 55
  • 1
    success is a callback function not a resolve promise function – joyBlanks Nov 20 '18 at 09:33
  • 1
    `return` causes its immediately containing function to return -- you are returning the object `{ min: ..., max: ... }` from the `success` function, whenever and however it is called -- that does not mean `$.ajax` has to return that or any other value, the two are completely unrelated in that manner. You need to understand how JavaScript works, basically. – Armen Michaeli Nov 20 '18 at 09:35
  • 1
    you can do something like `getLabels(min, max).then(sucessfn)` where you can do your operation of data. Dont forget to return $.ajax from getLabels function – joyBlanks Nov 20 '18 at 09:36
  • 1
    https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call – Oram Nov 20 '18 at 09:37
  • Thank you very much, all! Shall I delete the question? Because of the duplication. – Toma Tomov Nov 20 '18 at 09:59

0 Answers0