Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Promisify QT callbacks to angularjs promise



  • Hi Folks,
    I have started learning AngularJS and QT by my own. And I'm trying to write a promisify function that QT uses to an angularjs $q promise, but can't seem to get my head around it.

    The QT promises are something like:

    method(arg, arg, callback);
    

    so if C++ had a method that would concatenate two strings the call would be:

    method('A', 'B', function(result) {
      console.log(result);  //would print 'AB'
    });
    

    I'd like to be able to promisify this method so something like this would work:

    var promiseMethod = $q.promisify(method);
    
    promiseMethod('A', 'B')
      .then(result) {
         console.log(result);
      }
    

    I started adding my decorator, but can't seem to get the actual method calls correct:

    .config(function($provide) {
          $provide.decorator('$q', function($delegate, $rootScope) {
            $delegate.qtPromisify = function(fn) {
    
              /// ARGH!  what goes here!, this don't work.
              return function() {
                var args = Array.prototype.slice.call(arguments);
                return $delegate(function(resolve, reject) {
                  var result = fn.apply(null, args);
                  resolve(result);
                });
              }
            };
            return $delegate;
          });
        })
    

    Can anyone share your suggestions on this??

    Thanks & Regards
    Tejaswini


Log in to reply