I'm pretty new to JavaScript but have had some other programming experience so I'm picking most of it up (somewhat) quickly. Promises do totally confuse me.
I have a promise that took me a while to cobble together, but I finally got it working. If I call this
var test
.
.
detectActive().then(function(result) {
if (result.isActive)
{ alert("ACTIVE"); }
})
The alert pops up with the correct result. But instead of the alert, if I try to set the variable 'test' that exists outside of the promise (try to set it inside the promise, where the alert is now) it doesn't have any effect. I'm pretty sure that it is a scope issue.
// (from comments)
console.log(arguments);
detectActive().then(function(result) {
if (result.isActive) {
arguments.push("from within promise");
}
});
arguments.push("after promise");
When I try the above first console.log shows empty Array []. arguments.push within the promise returns a log error 'TypeError: arguments.push is not a function'. But the push outside the promise works and logs Array [ "after promise", "other data in the array" ].
What I actually want to do is run the detectActive function and then push a value to an array that exists in the outer function. I think I need to nest another promise. I tried many different versions of this idea all produce code that doesn't run. As I said earlier it took me along time to get the single promise to work.
What is the correct way to achieve what I want?