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



我有此代码:

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

如何在其自己的回调函数内使用roaming_prices_from的返回值?

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

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

为此,您必须在roaming_prices_from函数内传递一个值

我们将假设函数roaming_prices_from将如下: -

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

您需要假设roaming_prices_from将以国家价值解析,即该功能将具有Promise.resolve(country),这意味着您的回调(打印出来的国家/地区)将接收country作为一个参数

最新更新