我被困在这个异步函数问题上。我需要使用从回调函数获得的数据设置一个全局变量。我怎样才能做到这一点?
var soundId;
soundcheck(soundId, function (num){getSound(soundId, num)});
//callback
function getSound (soundId, result) {
var res = result;
if (!soundId) {
soundId = res;
}
console.log(soundId);
};
function soundcheck(soundId, callback) {
var lpid = $("a#entered_user_link").attr('href').substring(6);
chrome.extension.sendMessage({lpid: lpid}, function(response) {
if (response.resp) {
check = response.resp;
callback(check);
}
});
};
// i need to put response in this variable to use it in future
console.log(soundId);