Javascript 如何在自己的回调函数中使用函数的返回值?

Javascript 如何在自己的回调函数中使用函数的返回值?,javascript,callback,Javascript,Callback,我有以下代码: $('#from_country').change(function () { var selected_from = $(this).find('option:selected'); var country = roaming_prices_from(selected_from.val(), function () { console.log(country); }); 如何在其自己的回调函数中使用漫游\u价格\u的返回值?将函数的返回

我有以下代码:

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

如何在其自己的回调函数中使用
漫游\u价格\u的返回值?

将函数的返回值作为回调参数传递

$('#from_country').change(function () {
    var selected_from = $(this).find('option:selected');
    var country = roaming_prices_from(selected_from.val(), function (xCountry) {
        console.log(xCountry);
    });
});
但要做到这一点,您必须在
函数的
漫游价格中传递一个值

我们将假定来自
的功能
漫游价格如下:-

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

您需要假设来自
roaming\u prices\u将与国家/地区值解析,即该函数将具有
Promise.resolve(国家/地区)
,这意味着您的回调(打印出国家/地区)将作为参数接收
country

将值作为参数传递给回调是
漫游价格的责任。您必须更改此函数,或者不要将此函数用作回调函数(即
回调函数(roaming_prices_from(selected_from.val());
),为什么在从
roaming_prices_from
方法调用时不能将
国家/地区
传递给回调方法?例如:
var country=roaming_prices_from(selected_from.val()),函数(国家){console.log(国家);}?这似乎不起作用,但如果这是正确的。那我可能在别的地方有毛病。