mrfr
mrfr

Reputation: 1794

How to use the return value of function in its own callback function?

I have this code:

$('#from_country').change(function () {
    var selected_from = $(this).find('option:selected');
    var country = roaming_prices_from(selected_from.val(), function () {
        console.log(country);
    });

How can I use the return value of roaming_prices_from inside of its own callback function?

Upvotes: 1

Views: 53

Answers (2)

Tom Teman
Tom Teman

Reputation: 2013

You need to assume that roaming_prices_from will resolve with the country value, i.e that function will have Promise.resolve(country), which then means your callback (that prints out country) will receive country as a paramter

Upvotes: 0

hassan
hassan

Reputation: 8288

by passing the returning value of your function as a callback parameters ,

$('#from_country').change(function () {
    var selected_from = $(this).find('option:selected');
    var country = roaming_prices_from(selected_from.val(), function (xCountry) {
        console.log(xCountry);
    });
});

but to do this you have to pass a value inside your roaming_prices_from function

we will assume that the function roaming_prices_from will be as following :-

function roaming_prices_from(value, callback) {
    .....
    callback(returnedValue);
    .....
    return returnedValue;
}

Upvotes: 2

Related Questions