Lightning action call inside setTimeout returns result only when window (tab) loses focus
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
2
down vote
favorite
I have a Lightning action call inside a recursive setTimeout
call. I call the action immediately and then every 3 seconds until the result it not null
.
I see my console.log('tick');
immediately. But I don't see my result (in either the if or the else block) until my window (or tab) loses focus. As soon as it does lose focus, it returns immediately and I see my results as intended.
It may or may not be worth mentioning this action call is inside the callback (the then(...) of a previous action, as denoted by my variable action2
.
It also may or may not be worth mentioning that this is all happening on a Lightning Out site.
Also, I'm following the excellent action execution pattern as described by the infamous Bob Buzzard here:
http://bobbuzzard.blogspot.com/2016/12/javascript-promises-in-lightning_30.html
setTimeout(function tick()
console.log('tick');
self.executeAction(component, action2)
.then($A.getCallback(result =>
if (result)
console.log(result);
else
console.log('not yet...');
setTimeout(tick, 3000)
));
, 0);
It undoubtedly has something to do with a missing or improperly placed $A.getcallBack()
?
Many thanks.
lightning-components lightning javascript action lightning-action
add a comment |Â
up vote
2
down vote
favorite
I have a Lightning action call inside a recursive setTimeout
call. I call the action immediately and then every 3 seconds until the result it not null
.
I see my console.log('tick');
immediately. But I don't see my result (in either the if or the else block) until my window (or tab) loses focus. As soon as it does lose focus, it returns immediately and I see my results as intended.
It may or may not be worth mentioning this action call is inside the callback (the then(...) of a previous action, as denoted by my variable action2
.
It also may or may not be worth mentioning that this is all happening on a Lightning Out site.
Also, I'm following the excellent action execution pattern as described by the infamous Bob Buzzard here:
http://bobbuzzard.blogspot.com/2016/12/javascript-promises-in-lightning_30.html
setTimeout(function tick()
console.log('tick');
self.executeAction(component, action2)
.then($A.getCallback(result =>
if (result)
console.log(result);
else
console.log('not yet...');
setTimeout(tick, 3000)
));
, 0);
It undoubtedly has something to do with a missing or improperly placed $A.getcallBack()
?
Many thanks.
lightning-components lightning javascript action lightning-action
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I have a Lightning action call inside a recursive setTimeout
call. I call the action immediately and then every 3 seconds until the result it not null
.
I see my console.log('tick');
immediately. But I don't see my result (in either the if or the else block) until my window (or tab) loses focus. As soon as it does lose focus, it returns immediately and I see my results as intended.
It may or may not be worth mentioning this action call is inside the callback (the then(...) of a previous action, as denoted by my variable action2
.
It also may or may not be worth mentioning that this is all happening on a Lightning Out site.
Also, I'm following the excellent action execution pattern as described by the infamous Bob Buzzard here:
http://bobbuzzard.blogspot.com/2016/12/javascript-promises-in-lightning_30.html
setTimeout(function tick()
console.log('tick');
self.executeAction(component, action2)
.then($A.getCallback(result =>
if (result)
console.log(result);
else
console.log('not yet...');
setTimeout(tick, 3000)
));
, 0);
It undoubtedly has something to do with a missing or improperly placed $A.getcallBack()
?
Many thanks.
lightning-components lightning javascript action lightning-action
I have a Lightning action call inside a recursive setTimeout
call. I call the action immediately and then every 3 seconds until the result it not null
.
I see my console.log('tick');
immediately. But I don't see my result (in either the if or the else block) until my window (or tab) loses focus. As soon as it does lose focus, it returns immediately and I see my results as intended.
It may or may not be worth mentioning this action call is inside the callback (the then(...) of a previous action, as denoted by my variable action2
.
It also may or may not be worth mentioning that this is all happening on a Lightning Out site.
Also, I'm following the excellent action execution pattern as described by the infamous Bob Buzzard here:
http://bobbuzzard.blogspot.com/2016/12/javascript-promises-in-lightning_30.html
setTimeout(function tick()
console.log('tick');
self.executeAction(component, action2)
.then($A.getCallback(result =>
if (result)
console.log(result);
else
console.log('not yet...');
setTimeout(tick, 3000)
));
, 0);
It undoubtedly has something to do with a missing or improperly placed $A.getcallBack()
?
Many thanks.
lightning-components lightning javascript action lightning-action
lightning-components lightning javascript action lightning-action
edited 2 days ago
asked 2 days ago
David
254
254
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
3
down vote
accepted
Yes, you're missing an $A.getCallback
. What happens is that you're putting the action in the event queue, but it won't get executed until another Aura event occurs (in this case, either the blur or focus event is causing this). As soon as you go asynchronous, you're outside of the Aura life cycle, so Aura can't respond to any more events until the next time you re-enter the life cycle. The exception to this rule are those Aura methods that expect a callback; those callbacks will always be in the correct part of the Aura life cycle.
In this case, the missing $A.getCallback is in the setTimeout function:
setTimeout($A.getCallback(function tick() { ...
This is why your log appears immediately, but the action doesn't fire until later. This is true for any situation where you're using a native JavaScript callback. This includes native setTimeout
, setInterval
, onload
/onerror
/onprogress
/etc events for loading files or XMLHttpRequest callouts, and so on.
I knew you'd come to my rescue, @sfdxfox. Thanks very much for the explanation!
â David
2 days ago
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
Yes, you're missing an $A.getCallback
. What happens is that you're putting the action in the event queue, but it won't get executed until another Aura event occurs (in this case, either the blur or focus event is causing this). As soon as you go asynchronous, you're outside of the Aura life cycle, so Aura can't respond to any more events until the next time you re-enter the life cycle. The exception to this rule are those Aura methods that expect a callback; those callbacks will always be in the correct part of the Aura life cycle.
In this case, the missing $A.getCallback is in the setTimeout function:
setTimeout($A.getCallback(function tick() { ...
This is why your log appears immediately, but the action doesn't fire until later. This is true for any situation where you're using a native JavaScript callback. This includes native setTimeout
, setInterval
, onload
/onerror
/onprogress
/etc events for loading files or XMLHttpRequest callouts, and so on.
I knew you'd come to my rescue, @sfdxfox. Thanks very much for the explanation!
â David
2 days ago
add a comment |Â
up vote
3
down vote
accepted
Yes, you're missing an $A.getCallback
. What happens is that you're putting the action in the event queue, but it won't get executed until another Aura event occurs (in this case, either the blur or focus event is causing this). As soon as you go asynchronous, you're outside of the Aura life cycle, so Aura can't respond to any more events until the next time you re-enter the life cycle. The exception to this rule are those Aura methods that expect a callback; those callbacks will always be in the correct part of the Aura life cycle.
In this case, the missing $A.getCallback is in the setTimeout function:
setTimeout($A.getCallback(function tick() { ...
This is why your log appears immediately, but the action doesn't fire until later. This is true for any situation where you're using a native JavaScript callback. This includes native setTimeout
, setInterval
, onload
/onerror
/onprogress
/etc events for loading files or XMLHttpRequest callouts, and so on.
I knew you'd come to my rescue, @sfdxfox. Thanks very much for the explanation!
â David
2 days ago
add a comment |Â
up vote
3
down vote
accepted
up vote
3
down vote
accepted
Yes, you're missing an $A.getCallback
. What happens is that you're putting the action in the event queue, but it won't get executed until another Aura event occurs (in this case, either the blur or focus event is causing this). As soon as you go asynchronous, you're outside of the Aura life cycle, so Aura can't respond to any more events until the next time you re-enter the life cycle. The exception to this rule are those Aura methods that expect a callback; those callbacks will always be in the correct part of the Aura life cycle.
In this case, the missing $A.getCallback is in the setTimeout function:
setTimeout($A.getCallback(function tick() { ...
This is why your log appears immediately, but the action doesn't fire until later. This is true for any situation where you're using a native JavaScript callback. This includes native setTimeout
, setInterval
, onload
/onerror
/onprogress
/etc events for loading files or XMLHttpRequest callouts, and so on.
Yes, you're missing an $A.getCallback
. What happens is that you're putting the action in the event queue, but it won't get executed until another Aura event occurs (in this case, either the blur or focus event is causing this). As soon as you go asynchronous, you're outside of the Aura life cycle, so Aura can't respond to any more events until the next time you re-enter the life cycle. The exception to this rule are those Aura methods that expect a callback; those callbacks will always be in the correct part of the Aura life cycle.
In this case, the missing $A.getCallback is in the setTimeout function:
setTimeout($A.getCallback(function tick() { ...
This is why your log appears immediately, but the action doesn't fire until later. This is true for any situation where you're using a native JavaScript callback. This includes native setTimeout
, setInterval
, onload
/onerror
/onprogress
/etc events for loading files or XMLHttpRequest callouts, and so on.
answered 2 days ago
sfdcfox
231k10178394
231k10178394
I knew you'd come to my rescue, @sfdxfox. Thanks very much for the explanation!
â David
2 days ago
add a comment |Â
I knew you'd come to my rescue, @sfdxfox. Thanks very much for the explanation!
â David
2 days ago
I knew you'd come to my rescue, @sfdxfox. Thanks very much for the explanation!
â David
2 days ago
I knew you'd come to my rescue, @sfdxfox. Thanks very much for the explanation!
â David
2 days ago
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f235050%2flightning-action-call-inside-settimeout-returns-result-only-when-window-tab-lo%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password