javascript - The function return value needs to be processed by the callback function of the internal method. How to write it?
给我你的怀抱
给我你的怀抱 2017-06-26 10:55:32
0
4
849

for example

function a(a,b){
    $.messager.confirm('xxx','xx',function(r){
        if(r){
            b = 1;
        }else{
            b = 2;
        }
    });
    return b;
}

I need to get the processed b, how should I change this function

给我你的怀抱
给我你的怀抱

reply all(4)
習慣沉默

The result of the callback function can only be sent out using the callback function.

function a(a,b,callback){
    $.messager.confirm('xxx','xx',function(r){
        if(r){
            b = 1;
        }else{
            b = 2;
        }
        callback(b)
    });
}
曾经蜡笔没有小新

Look at the form$.messager.confirm is an asynchronous call, you can wrap this call with a layer of promise;

function a(a,b) {
    return new Promise(function(resolve, reject) {
        $.messager.confirm('xxx','xx',function(r){
            if (r) {
                b = 1;
            } else{
                b = 2;
            }
            resolve(b);
        });
    });
}

When calling a, you can call it as follows

a(xxx, xxx).then(function(b) {
    // b就是上面resolve的值
});
洪涛
function a(a,b){
    let t;
    $.messager.confirm('xxx','xx',function(r){
        if(r){
            t = 1;
        }else{
            t = 2;
        }
    });
    return t;
}

//或者
function a(a,b){
    $.messager.confirm('xxx','xx',function(r){
        if(r){
            return  1;
        }else{
            return  2;
        }
    });
}
三叔

The callback function is executed after your main function is executed, which means that you must first have two parameters, xxx and xx, and then the callback is executed. There is nothing wrong with your function.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template