为什么我的$ q.defer()。解析不起作用?

时间:2022-08-23 14:48:37

As the demo shows,when I define my promise in service and return it back(promise1 in the demo),it doesn't resolve at all.

正如演示所示,当我在服务中定义我的承诺并将其返回(演示中的promise1)时,它根本无法解决。

But when I define the promise(promise2 in the demo) in controller,it works good.Why?

但是当我在控制器中定义承诺(演示中的promise2)时,它运行良好。为什么?

this is my demo on codepen

这是我在codepen上的演示

serv.getDefer = function() {
    var defer = $q.defer();
    return {
      defer: defer,
      promise: defer.promise
    }
}

var defer1 = serv.getDefer().defer;
var promise1 = serv.getDefer().promise;
promise1.then(function() {
    alert('promise1 should work,but doesnt')
})
defer1.resolve();

var defer2 = serv.getDefer().defer;
var promise2 = defer2.promise;
promise2.then(function() {
    alert('promise2 works good')
})
defer2.resolve();

3 个解决方案

#1


2  

Look into closures, the second time you call the function getDefer(), it makes a new variable, and returns it.

查看闭包,第二次调用函数getDefer()时,它会创建一个新变量并返回它。

However, this might work (not tested though)

但是,这可能会起作用(尽管没有经过测试)

var deferred = serv.getDefer();
deferred.promise.then(function() {
    alert('promise1 should work,but doesnt')
});
deferred.defer.resolve();

#2


2  

Every time you call serv.getDefer() it returns a new deffered object, in your promise 1 you call serv.getDefer() both to create defer1 and promise1. The 2 variables now aren't the same promise, so when you resolve defer1, the promise of promise1 is still unresolved.

每次调用serv.getDefer()时,它都会返回一个新的deffered对象,在你的promise 1中你调用serv.getDefer()来创建defer1和promise1。现在2个变量不是相同的promise,所以当你解析defer1时,promise1的promise仍然没有得到解决。

in your second example promise2 is the promise of defer2, so when you resolve defer2 the promise will be resolved.

在你的第二个例子中,promise2是defer2的承诺,所以当你解决defer2时,promise将被解决。

to fix this you have to do the following:

要解决此问题,您必须执行以下操作:

var deferred = serv.getDefer(),
    defer1 = deferred.defer
    promise1 = deferred.promise;

promise1.then(function() {
  alert('promise1 should work,but doesnt')
});

defer1.resolve();

#3


1  

promise1 is not generated by defer1. Instead, you are creating a new deferred object by calling getDefer() again.

promise1不是由defer1生成的。相反,您通过再次调用getDefer()来创建新的延迟对象。

promise2 works because it is generated by defer2.promise()

promise2有效,因为它是由defer2.promise()生成的

I think the line should be

我认为这条线应该是

var promise1 = defer1.promise;

#1


2  

Look into closures, the second time you call the function getDefer(), it makes a new variable, and returns it.

查看闭包,第二次调用函数getDefer()时,它会创建一个新变量并返回它。

However, this might work (not tested though)

但是,这可能会起作用(尽管没有经过测试)

var deferred = serv.getDefer();
deferred.promise.then(function() {
    alert('promise1 should work,but doesnt')
});
deferred.defer.resolve();

#2


2  

Every time you call serv.getDefer() it returns a new deffered object, in your promise 1 you call serv.getDefer() both to create defer1 and promise1. The 2 variables now aren't the same promise, so when you resolve defer1, the promise of promise1 is still unresolved.

每次调用serv.getDefer()时,它都会返回一个新的deffered对象,在你的promise 1中你调用serv.getDefer()来创建defer1和promise1。现在2个变量不是相同的promise,所以当你解析defer1时,promise1的promise仍然没有得到解决。

in your second example promise2 is the promise of defer2, so when you resolve defer2 the promise will be resolved.

在你的第二个例子中,promise2是defer2的承诺,所以当你解决defer2时,promise将被解决。

to fix this you have to do the following:

要解决此问题,您必须执行以下操作:

var deferred = serv.getDefer(),
    defer1 = deferred.defer
    promise1 = deferred.promise;

promise1.then(function() {
  alert('promise1 should work,but doesnt')
});

defer1.resolve();

#3


1  

promise1 is not generated by defer1. Instead, you are creating a new deferred object by calling getDefer() again.

promise1不是由defer1生成的。相反,您通过再次调用getDefer()来创建新的延迟对象。

promise2 works because it is generated by defer2.promise()

promise2有效,因为它是由defer2.promise()生成的

I think the line should be

我认为这条线应该是

var promise1 = defer1.promise;