INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Promise not working

Promise not working

(OP)
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 () {

   var deferred = new $.Deferred();

   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();
   }

   deferred.resolve(null);
   return deferred.promise();
} 

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

RE: Promise not working

(OP)
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.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close