-1

I am using promise in angular for my web app like this-

var deferred = $q.defer();

On Success -

deferred.resolve(profile); // profile = JSON object

On Failure -

deferred.reject(1); // 1 or no value returned

At end of function -

return deferred.promise;

Then I am handing for this returned Promise Object to call another method.But it doesn't call. While if i use Callback(error,success) it works fine.Can somebody suggest what is wrong with my promise.

Code Snippet-

  function open() {    // for initializing DB,getting called from service
    var deferred = $q.defer();
    var options = {
      Encryption: {
        encryptKey: false,   // optional encrypt primary key
        secrets: [{
            name: 'dddd',
            key: 'xxxxxxxxxx'
        }]
      }
    };

    var schema = {
      stores:[{
        name:'profile',
        encrypted: true
      }]
    };

    var db = new ydn.db.Storage('nowconferdb', schema, options);

    db.onReady(function() {
        console.log('DB is initialized'); // getting this 
        profilestorage.setDB(db); // getting called and setting DB in profilestorage service
        deferred.resolve(true);
    });

    db.addEventListener('fail', function (event) {
        var err = event.getError();
        if (err.name == 'versionchange') {
            console.log('The application is updated, please refresh to upgrade.');
            profilestorage.setup(db);
        } else {
            console.log('connection failed with ' + err.name + ' by ' + err.message);
            db = null; // no operation can be placed to the database instance
        }

        deferred.reject(false);
    });

    return deferred.promise; 
}

This is my calling method -

storageservice.open().then(function() {
  console.log('post initializing storageservice'); // not getting it.
});

Thanks a lot for your sincere efforts.

Kishor
  • 201
  • 1
  • 10
  • 2
    Can you please provide your function, or at least parts of so we can get a bit more context for your problem. – cbass Sep 02 '16 at 11:51
  • How are you calling next function ? Can you paste line where you are calling another function after receiving promise. – mkkhedawat Sep 02 '16 at 12:01
  • Can you post a fiddle?It is hard to understand what is the problem source. – Itsik Mauyhas Sep 02 '16 at 12:25
  • Show us the actual code where you're using the returned promise. That is likely where you mistake is. – jfriend00 Sep 02 '16 at 15:28
  • Unable to add code in comments,Please go to DB initialization section in this link (same issue- not getting "post initializing storageservice" log after promise) http://stackoverflow.com/questions/39168002/ydn-db-include-issue – Kishor Sep 04 '16 at 07:14
  • You use the "Edit" link to add code to your question, not to comments. – jfriend00 Sep 05 '16 at 00:31

1 Answers1

2

What you should do is to call your function inside the success callback of the promise. Assumming you assign your promise to deferred variable:

deferred.then(
    function (data) {
        // Here you call your other function/method
        // It will be called when 'deferred' promise is resolved
        anotherFunction();
    },

    function (error) {
        // Handle the error
    }
);

I hope this helps, even though the information you give is not enough.

Alvaro Vazquez
  • 372
  • 3
  • 9
  • Ya Alvaro,this is standard approach. Unable to add code in comments,Please go to DB initialization code in this link (same issue- not getting "post initializing storageservice" log after promise) http://stackoverflow.com/questions/39168002/ydn-db-include-issue – Kishor Sep 03 '16 at 23:18