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
習慣沉默2017-06-26 10:57:32
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)
});
}
曾经蜡笔没有小新2017-06-26 10:57:32
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的值
});
PHP中文网2017-06-26 10:57:32
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;
}
});
}
三叔2017-06-26 10:57:32
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.