angularJS中$ q.when和$ q.defer之间的差异

时间:2022-12-04 15:15:00

I am seeing angularJS files where in some places reviewer has commented these 3 lines:

我看到angularJS文件,在某些地方,评论者评论了这3行:

var deferred = $q.defer();
deferred.resolve(BrandList);
return deferred.promise;

and replaced with this one:

并替换为这一个:

return $q.when(BrandList);

I would like to understand the difference between two. Do both serve same purpose? Which should be used when?

我想了解两者之间的区别。两者都有同样的目的吗?哪个应该用?

2 个解决方案

#1


4  

Ben Nadal does a great job of explaining this here.

本纳达尔在这里做了很好的解释。

In his words:

用他的话说:

The $q.when() method doesn't just create a promise that is immediately resolved; rather, it normalizes a value that may or may not be a "thenable" object. If the given value is a promise, $q.when() will properly chain off of it. If the given value is not a promise, $q.when() will create promise resolved with the given value.

$ q.when()方法不只是创建一个立即解决的promise;相反,它将一个值标准化,该值可能是也可能不是“可以”的对象。如果给定的值是一个promise,$ q.when()将正确地链接它。如果给定的值不是promise,$ q.when()将创建使用给定值解析的promise。

So essentially it takes either promises or non-promises and does some magic to make sure they are either resolved or promise wrapped so as not to break the promise chain.

所以基本上它需要承诺或非承诺,并做一些魔术,以确保它们被解决或承诺包装,以便不打破承诺链。

#2


0  

$q.defer() is appropriate when you want to decorate a function to use promises instead of callbacks. Usually, you will specify a deferred.resolve() and a deferred.reject(), and maybe even a deferred.notify().

当你想装饰一个函数来使用promises而不是回调时,$ q.defer()是合适的。通常,您将指定deferred.resolve()和deferred.reject(),甚至可能是deferred.notify()。

$q.when() is appropriate when you want to immediately create a new promise and resolve it to a value. EDIT it also normalizes variables into promises, which is useful if the variable may or may not be a promise. See Jim's answer.

当您想立即创建新承诺并将其解析为值时,$ q.when()是合适的。编辑它还可以将变量规范化为承诺,如果变量可能是也可能不是承诺,这很有用。看到吉姆的回答。

$q.when() seems to be appropriate for your case.

$ q.when()似乎适合你的情况。

#1


4  

Ben Nadal does a great job of explaining this here.

本纳达尔在这里做了很好的解释。

In his words:

用他的话说:

The $q.when() method doesn't just create a promise that is immediately resolved; rather, it normalizes a value that may or may not be a "thenable" object. If the given value is a promise, $q.when() will properly chain off of it. If the given value is not a promise, $q.when() will create promise resolved with the given value.

$ q.when()方法不只是创建一个立即解决的promise;相反,它将一个值标准化,该值可能是也可能不是“可以”的对象。如果给定的值是一个promise,$ q.when()将正确地链接它。如果给定的值不是promise,$ q.when()将创建使用给定值解析的promise。

So essentially it takes either promises or non-promises and does some magic to make sure they are either resolved or promise wrapped so as not to break the promise chain.

所以基本上它需要承诺或非承诺,并做一些魔术,以确保它们被解决或承诺包装,以便不打破承诺链。

#2


0  

$q.defer() is appropriate when you want to decorate a function to use promises instead of callbacks. Usually, you will specify a deferred.resolve() and a deferred.reject(), and maybe even a deferred.notify().

当你想装饰一个函数来使用promises而不是回调时,$ q.defer()是合适的。通常,您将指定deferred.resolve()和deferred.reject(),甚至可能是deferred.notify()。

$q.when() is appropriate when you want to immediately create a new promise and resolve it to a value. EDIT it also normalizes variables into promises, which is useful if the variable may or may not be a promise. See Jim's answer.

当您想立即创建新承诺并将其解析为值时,$ q.when()是合适的。编辑它还可以将变量规范化为承诺,如果变量可能是也可能不是承诺,这很有用。看到吉姆的回答。

$q.when() seems to be appropriate for your case.

$ q.when()似乎适合你的情况。