0

This is just pseudo code , please bear in mind this, this is just generic question.

  var xhrRequests = {};
  
  function MainMethod() {
      var waitFor;
      if(someCondition..)
      {
          waitFor =  GetFuncAsyncB();
      }
      else
      {
        waitFor = Promise.resolve(undefined);
      }

      waitFor.then(function () {;
      ......Code Blocks needs to executed after promise... 
       });
  }

function B:

function GetFuncAsyncB() {
                                   
           var  getPromise ;
           getPromise = GetFuncAsyncA();

           return getPromise.then (function (data: ObjectType) {

            ....... 

            }).catch((error) => {

            console.log("Failed" + error);

            });
 
 }

 

function A:

  function GetFuncAsyncA() {

    return new Promise(function (resolve, reject) {
                url = .....;
                var queryObj = {
                    method: "GET",
                    url: url,
                    cache: false,
                    dataType: 'json'
                };
    
                xhrRequests[url] = $.ajax(queryObj)
                    .done(querySucceeded)
                    .fail(queryFailed);
    
                function querySucceeded(data) {
                    
                    resolve(data);
                }
    
                function queryFailed(jqXHR, textStatus, errorThrown) {
                    if (xhrRequests.hasOwnProperty(url)) {
                       delete xhrRequests[url];
                    }

                    if (errorThrown !== "abort") {
                        
                    }
                    reject(jqXHR);
                }
            });
    
    
        }

How can I make the code block between GetFuncAsync.then (function (data: ObjectType) {.... }) run just after it is called. I do not want after codes... to be run before a code block inside .then execute? How can I achieve this? (I can not change function GetFuncAsync() method. ) The problem in my side is the code after getPromise.then clears the xhrRequests list so my code always drops to queryFailed. If I can make getPromise.then run just after getPromise = GetFuncAsync statement, my problem will be sorted out.

And more thing I realize, instead of getPromise.then if I write another function such as getPromise.xxx , the debugger still does not find error. I know I am doing stg wrong, if anyone can help me, I would be very grateful

Ozmen
  • 129
  • 3
  • 10
  • Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackoverflow.com/rooms/249278/discussion-on-question-by-ozmen-how-make-promise-then-run-after-called). – Samuel Liew Nov 03 '22 at 13:01

0 Answers0