0

I am facing problem when I am assigning value of a variavle from outside of a chrome api like the example below.

Code:

var btnStatus;

chrome.runtime.sendMessage({jsBlocker: "check"}, function(response) {
 btnStatus = response.data;
});

alert(btnStatus );

Output:

Output

1 Answer 1

2

sendMessage is asynchronous, which means that it will execute a passed callback only when it's ready, so it will take some time for a statement btnStatus = response.data; to happen. Again, because it is asynchronous the code below won't wait for sendMessage and will continue to run, as a result alert will show you the initial value of the btnStatus which is undefined. All your code which uses the data from response is dependent on this callback. You could put an alert inside the callback to test a coming data and then do desired stuff with it:

chrome.runtime.sendMessage({jsBlocker: "check"}, function(response) {
 btnStatus = response.data;
 alert(btnStatus);
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.