JavaScript AngularJS and Q.fcall angularjs call function,angularjs callback,angularjs call function from html,

Angulars $q is a promise/deferred implementation inspired by Kris Kowal's Q.

In Q, you create a promise with

var myPromise = Q.fcall(myFunction);

Here the myFunction will be invoked asynchronously, a promise is placed in the myPromise variable and the code execution continues.

The only example Angular gives for creating a promise is using javascript timeout function, which to me seems like a verbose hack compared to the above Q example. So in Angular I would write

function asyncWorker(name) {
  var deferred = $q.defer();

  setTimeout(function() {
    scope.$apply(function() {
      deferred.resolve(myFunction);
    });
  }, 1000);

  return deferred.promise;
}

The above would be identical to the one-liner at the top.

I hoped that $q.fcall would have worked but I get:

TypeError: 'undefined' is not a function (evaluating '$q.fcall(function() { return 'a'; })')

So what's the most straightforward way of asynchronously invoking a function and returning a promise in AngularJS?

Answer:1

I don't know if this is very smart, but it works for me:

function fcall(someValues) {
    var deferrd = $q.defer();
    deferrd.resolve(someValues);
    return deferrd.promise;
}

fcall(123).then(function(values) {
    console.log(values); // 123
});
Answer:2

Ok, cleaner alternative is injecting Angular's $timeout and let's say again that function myFunction is the work I want to do asynchroniously, I'll just do:

function doWorkAsync() {
   return $timeout(myFunction, 10);
}

and doWorkAsync will return a promise that will be resolved when myFunction has finished it's work.

For unit testing I can call $timeout.flush() to fire the timeout function instantly.

Answer:3
    function createPromise(func) {
        return function (params) {
            var deferred = $q.defer();

            $timeout(function () {
                try {
                    var rtn = func(params);
                    deferred.resolve(rtn);
                }
                catch (ex) {
                    deferred.reject();
                }

            })
            return deferred.promise
        }
    }
Answer:4

You can use $q as a constructor similar to how ES6 works.

function asyncGreet(name) {
  return $q(function(resolve, reject) {
    resolve('Hello, ' + name + '!');
  });
}

angular docs

Answer:5

I'm using backbone js in my Project, I'm struck in a small confusion with views. I'm having these 2 views. After writing them i am in a confusion whether i'm in right path or not. The reason for my ...

I'm using backbone js in my Project, I'm struck in a small confusion with views. I'm having these 2 views. After writing them i am in a confusion whether i'm in right path or not. The reason for my ...

I know there is a great difference in 2 methods below: a.onclick = function() {alert("Hi");}; and a.onclick = (function() {alert("Hi");};)(); Why do people use the second method? The second should ...

I know there is a great difference in 2 methods below: a.onclick = function() {alert("Hi");}; and a.onclick = (function() {alert("Hi");};)(); Why do people use the second method? The second should ...

I have 2 multiple select boxes where I select some of the options and add it to the other select box. When i click on save, the selected option values should be stored into one array and passed to the ...

I have 2 multiple select boxes where I select some of the options and add it to the other select box. When i click on save, the selected option values should be stored into one array and passed to the ...

  1. options selected value javascript
  2. options selected value jquery
  3. options selected value
  4. get selected options value jquery
  5. get selected options value javascript
  6. get selected option value
  7. ng-options selected value
  8. ng-options selected value angularjs
  9. ng-options selected value not displaying
  10. html options selected value
  11. knockout options selected value
  12. ng options selected value jsfiddle
  13. ng-options selected value not working
  14. angular select options selected value
  15. document.getelementbyid.options selected value
  16. angularjs select options selected value
  17. javascript set options selected value
  18. angular ng-options selected value

I'm trying to create some isolation in javascript using the module pattern. but when i run this code: var mymap = {}; mymap['230'] = (function () { privatePageLoad = function(nid, page){ ...

I'm trying to create some isolation in javascript using the module pattern. but when i run this code: var mymap = {}; mymap['230'] = (function () { privatePageLoad = function(nid, page){ ...

  1. javascript module pattern es6
  2. javascript module pattern example
  3. javascript module pattern with jquery
  4. javascript module pattern vs class
  5. javascript module pattern w3schools
  6. javascript module pattern vs revealing module pattern
  7. javascript module pattern inheritance
  8. javascript module pattern constructor
  9. javascript module pattern mdn
  10. javascript module pattern private variables
  11. javascript module pattern vs prototype
  12. javascript module pattern import
  13. javascript module pattern document ready
  14. javascript module pattern private function
  15. javascript module pattern event handler
  16. javascript module pattern vs object literal
  17. javascript module pattern with parameters
  18. javascript module pattern tutorial