If b
is a synchronoys method, you simply store the value in a variable, so that you can return it from the save_current_side
function instead of from the callback function:
function save_current_side(current_side) {
var result;
a.b({
callback: function (a) {
result = a;
}
});
return result;
}
If b
is an asynchronous method, you can't return the value from the function, as it doesn't exist yet when you exit the function. Use a callback:
function save_current_side(current_side, callback) {
a.b({
callback: function (a) {
callback(a);
}
});
}
save_current_side(current_side, function(a){
console.log(a);
});
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…