0

I have an angularjs application launched inside an overlay from another non Angular application. In the application, i am hitting a xhr request from inside a setTimeout() with delay of say 10 secs. The xhr request is sent to a device attached to system and performs an action on device. After calling setTimeout(), i need to hide the overlay (display:hidden) and unmount my application from DOM entirely without waiting for timeout delay. [refer code].

The problem is, xhr request operation is executed after set delay(checked using breakpoint), its response (a promise) is logged in console. But, promise callbacks - resolve or rejected are not executed, network tab does not show the http call, device does not perform desired action.

if i remove/comment unMountOverlay(), then code works as expected. Also i cant wait for timeout delay to execute unMountOverlay().

callTimeOut() {  /** called on some user action */
    var self = this;
    console.log("Calling Timeout"); /** Getting printed */
    setTimeout(()=>{
        ayncOperation().then(()=>{
            console.log("Success"); /** Not Getting printed */
        },
        ()=>{
            console.log("Failed"); /** Not Getting printed */
        })
    },
    10000);
    unMountOverlay(); // set display:none; Unmount application from DOM
}

ayncOperation(){
    let promise = this.request(config.launch);   /** call for async operation to device */
    console.log(promise);  /** Getting printed */ /** a promise object containing methods - then catch finally .. */
    return promise;
}

i have 2 questions .. 1) When xhr request is getting executed and response promised is logged in console, why the http request is not visible in network tab ? 2) When a promise object is being returned and logged in console, why resolution/rejection callbacks are not executed.

As per my understanding, when runtime engine finds settimeout(), it starts the counter, adds settimeout to stack, executes code further. When timeout delay is over, pops the timeout callback and executes the same, even when the rest of application has exited. Please correct me.

P.S. My application only works in IE.

Mohit
  • 19
  • 3
  • 1
    `ayncOperation` is a function, not a Promise, it doesn't have a `.then` method - your current code will result in a TypeError. You must call the function so that it runs and returns the Promise – CertainPerformance Sep 07 '19 at 06:14
  • The code is for reference, i cant share the actual code over public platforms, Please ignore some minor syntax issues and try to understand the problem mentioned. – Mohit Sep 07 '19 at 06:19
  • Without a [MCVE] (which includes the **actual code**), it's impossible to say – CertainPerformance Sep 07 '19 at 06:20
  • @CertainPerformance Could you please link the original question here, on basis of which this question has been marked duplicate. Thanks in advance. – Mohit Sep 07 '19 at 06:22
  • I did - look at the top of the page – CertainPerformance Sep 07 '19 at 06:23
  • @CertainPerformance the question linked does not relate to my question in any way. The question is not about parenthesis. Please read the question and details. – Mohit Sep 07 '19 at 15:21
  • Now that you've fixed the parentheses, I can't reproduce, your code appears to work using a dummy Promise-returning function https://jsfiddle.net/ocdvLf9t/ – CertainPerformance Sep 07 '19 at 18:31

0 Answers0