Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Promise not working

Status
Not open for further replies.

tfstom

Programmer
Sep 28, 2002
190
US
I am using PouchDB to handle some local database processing and I am having an issue setting up my promise to work correctly.

In essence I have the following:
Code:
...
   }).then(function () {
      return initialize();
   }).then(function () {
      return updateWorkIssuesJson();
   }).then(function () {
      return showTasks();
   }).catch(function (err) {
      showMsg("Error in defineDBs: " + err);
   })

In "initialize" I have a couple of async routines that read from the PouchDB database. I turned initialize into a promise to stop it from going to "updateWorkIssuesJson" and then "showtasks" before initialize was finished. But it still jumps out of initialize before it is finished. All I did was create an anonymous routine and put a couple of "Deferred" statements in the but it still jumps out when it hits the Async routines.

Am I doing something wrong?

My code is a little large but it really isn't the issue.

I just took the "initialize" function and made it variable that has the function in it and added the promise (deferred) code around it.

Code:
var initialize = function () {

 [COLOR=#EF2929]  var deferred = new $.Deferred();
[/color]
   initBreadCrumbs();
   initControls();
   initCalendarControls();
   initAutoCompleteControls();
   initTreeViewControls();
   initDropDowns();
   // Don't let Enter key submit
   $('form').keypress(function (e) {
      if (e.which === 13) {
         return false;
      }
   });

   initImageFunctions();

   if (CTL_HiddenSyncDatabase.val() == "True") {
      return callDBDestroyDefineAndLoad(false).then(function () {
         alert("Syncing database");
         CTL_HiddenSyncDatabase.val("False");
         DirtyFlag = false;
         DirtyCertFlag = false;
      })
   }
   else if (CTL_HiddenFirstTime.val() == "True") {
      var updated = [];
      CTL_HiddenFirstTime.val("False");

      return DB_WorkIssues.allDocs({ include_docs: true, descending: false }, function (err, response) {
         data = response.rows;
         itemp = 0;

         for (var i = 0; i < response.total_rows; i++) {
            if (data[i].doc.IsDeleted || data[i].doc.IsWorkIssueInserted || data[i].doc.IsWorkIssueUpdated || data[i].doc.IsLogInserted) {
               DirtyFlag = true;
               updated[itemp++] = data[i];
            }
         }
         CTL_HiddenJsonWorkIssues.val(JSON.stringify(updated));
      }).then(function () {
         return DB_Certificates.allDocs({ include_docs: true, descending: false }, function (err, response) {
            data = response.rows;
            updated = [];
            itemp = 0;

            for (var i = 0; i < response.total_rows; i++) {
               if (data[i].doc.IsUpdated) {
                  DirtyCertFlag = true;
                  updated[itemp++] = data[i];
               }
            }
            CTL_HiddenJsonCertificates.val(JSON.stringify(updated));

            if (DirtyFlag || DirtyCertFlag) {
               GetDirtyRecords();
            }

            ReloadWorkIssues();
         });
      });

   }
   else if (CTL_HiddenSyncWorklist.val() == "True") {
      CTL_HiddenSyncWorklist.val("False");
      ReloadWorkIssues();
   }

[COLOR=#EF2929]   deferred.resolve(null);
   return deferred.promise();
[/color]}

I have breaks in my code and I am at the first "for" statement and then it jumps to the "updateWorkIssuesJson" function then back again.

Is there something else I need to do?

Thanks,

Tom
 
I was under the assumption (maybe incorrectly) that the deferred code around the code would prevent the code from jumping outside of the promise chains. I understand it would jump to something else but would not follow the "then" chains until function was done.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top