Jquery 处理来自ajax请求返回的数据,但在回调函数之外

Jquery 处理来自ajax请求返回的数据,但在回调函数之外,jquery,ajax,function,callback,getjson,Jquery,Ajax,Function,Callback,Getjson,我有这个密码 /* Get the source of the data. */ if (if_the_source_is_an_url) { $.getJSON(url_here, function(returnedData){ theData = returnedData; }); } /* Here we have the default processing of theData. */ 所以我需要,如果用户提供一个url,那么将在回调函数之外处理返回的数据,因

我有这个密码

/*
   Get the source of the data.
*/

if (if_the_source_is_an_url) {

  $.getJSON(url_here, function(returnedData){ theData = returnedData; });

}

/*
   Here we have the default processing of theData.
*/
所以我需要,如果用户提供一个url,那么将在回调函数之外处理返回的数据,因为我需要重用数据处理代码。所以,我有了这个想法

  • 使用break命令
    $.getJSON(url\u在此,
    函数(返回数据){theData=
    返回的数据;中断;})
  • 更改为$.ajax请求,但使用 异步:false
  • 只需将数据处理放在 函数并从
    回调函数

    if(url)$.getJSON(url\u此处,
    函数(返回的数据){
    自定义函数(返回数据);});
    else自定义函数(defaultData)


如果这能起作用(我没有测试它,因为我不在家),或者有更好的实践吗?

附加处理程序/函数以过滤/转换数据。所有ajax请求都将调用此处附加的句柄

我认为第三个选项(从回调调用数据处理函数)是最好的继续方式。我将采用您的解决方案。谢谢