Making Your Own Corner $ q Promises

This example attempts to make synchronous code asynchronous. All the examples I found did the opposite, with the exception of the main first example in docs.angularjs.org .. $ q below.

The document contains the $ q constructor that I am trying to use. Unfortunately, the jsfiddle Angular 1.1.1 and 1.2.1 libraries provide a $ q object (not a function), as in this example. Instead, I should give my example and hope that someone sees a mistake.

https://docs.angularjs.org/api/ng/service/ $ q

I need to see that "this does not happen!" for execution.

f = function(name) {
    return $q(function(resolve, reject) {
        console.log "this does not happen!"
        resolve('great')
    });
}


f(name).then(function(result) {
  console.log 'result', result
}, function(error) {
  console.log 'error', error
});

Instead of registering, this does not happen! after which "excellent", I really see the function passed in $ q logged ::

    result function(resolve, reject) {
        console.log "this does not happen!"
        resolve('great')
    }

- , ?

+4
2

. , angular , :

, ES6 Harmony promises .

, .

(, Zipper)

var dfd = $q.defer();

:

dfd.reject('some value');
dfd.resolve('some value');
+4

, , , .

f = function(someValue){
    var deferred = $q.defer();
    doSomethingAsync(someValue, function(result){ return deferred.resolve(result)});
    return deferred.promise;
}

f("foo").then(function() {alert("I was called after the async thing happened")})
+5

All Articles