1

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?

3
  • Passing the value as argument to the callback is the responsibility of roaming_prices_from. You must change this function, or don't use the function as callback (i.e. callback(roaming_prices_from(selected_from.val()));) Commented Feb 28, 2017 at 12:05
  • Why can't you just pass the country to callback method when invoking it from roaming_prices_from method? Commented Feb 28, 2017 at 12:05
  • like such: var country = roaming_prices_from(selected_from.val(), function (country) { console.log(country); }); ? This dosnt seem to work, but if this is correct. Then I might have a fault somewhere else. Commented Feb 28, 2017 at 12:06

2 Answers 2

2

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;
}
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.