Js ajax request problem.

in the project, request background data through ajax and do data processing. Finally, the processed data will be returned. The code looks like this:

function funOne(){
    var htmlStr = "";
    $.get(href,{},function(r){
        var data = eval("(" + r + ")");
        $.each(data,function(k,v){
            /*     */
            htmlStr += "<div>" + v + "</div>";
        })
    })
    return htmlStr;
}
ajax 
 while 
Feb.28,2021

write

var getData = new Promise(function (resolve, reject) {
    $.get(href, {}).then(function (res) {
        if (res) {
            var data = JSON.parse(res);
            //   htmlStr ...
            resolve(htmlStr);
        } else {
            reject(err);
        }
    })
})

getData.then((htmlStr ) => {
    console.log(htmlStr )
}).catch((err) => {
    console.log(err)
})

$.get (href, {}, function (r) {

)
var data = eval('(' + r + ')');
$.each(data,function(k,v){
    /*     */
    htmlStr += '<div>' + v + '</div>';
})
 return htmlStr;

})


function funOne() {
    var htmlStr = '';
    return $.get(href, {}).then(function (r) {
        var data = eval('(' + r + ')');
        $.each(data, function (k, v) {
            /*     */
            htmlStr += '<div>' + v + '</div>';
        })
        return htmlStr;
    })
}

funOne().then(function(htmlStr){
    console.log(htmlStr)
})
Menu