$q.defer() then of the then [英] $q.defer() then of the then

查看:25
本文介绍了$q.defer() then of the then的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白 then 的返回值的行为,

这里是文档 -

<块引用>

then(successCallback, errorCallback, notifyCallback) – 不管当承诺已经或将被解决或拒绝时,然后调用一个异步成功或错误回调的结果可用.使用单个参数调用回调:结果或拒绝原因.

所以让我们尝试一下,根据下面的小代码部分 -

var deferred = $q.defer();deferred.reject();deferred.promise.then(function () {console.log("1st resolove");},功能 () {console.log("第一次拒绝");}).then(函数(){console.log("第二次解决");},功能 () {console.log("第二次拒绝");});

1) 为什么会记录 -

第一次拒绝第二个决心

而不是 -

第一次拒绝第二次拒绝

?

2) 我需要改变什么才能让它记录 -

第一次拒绝第二次拒绝

?

var myAppModule = angular.module('myApp', []).控制器('myCtrl',function($scope,$q){var deferred = $q.defer();//deferred.resolve();deferred.reject();deferred.promise.then(function () {console.log("1st resolove");},功能 () {console.log("第一次拒绝");}).then(函数(){console.log("第二次解决");},功能 () {console.log("第二次拒绝");});});

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.20/angular.min.js"></script><div ng-app="myApp"><div ng-controller="myCtrl"></div>

解决方案

您错过了文档的下一行:

<块引用>

此方法返回一个新的承诺,该承诺通过以下方式解决或拒绝successCallback 的返回值,errorCallback.

如果您希望下一个链无论出于何种原因都是相同 promise 的结果,请从处理程序内部返回 deferred.promise.

//这将始终触发拒绝回调deferred.promise.then(function () {console.log("1st resolove");返回 deferred.promise;},功能 () {console.log("第一次拒绝");返回 deferred.promise;})

如果你想返回成功/失败的回调,取决于第一个的结果,return $q.when() 在你的成功和 return $q.reject() 在你的失败中:

deferred.promise.then(function () {console.log("1st resolove");返回 $q.when();},功能 () {console.log("第一次拒绝");返回 $q.reject();}).then(函数(){console.log("第二次解决");},功能 () {console.log("第二次拒绝");});

示例

I didn't understand the behavior of the return value of then ,

Here its documentation -

then(successCallback, errorCallback, notifyCallback) – regardless of when the promise was or will be resolved or rejected, then calls one of the success or error callbacks asynchronously as soon as the result is available. The callbacks are called with a single argument: the result or rejection reason.

So let's try it , according to the follow little code section -

var deferred = $q.defer();
deferred.reject();
deferred.promise.then(function () {
        console.log("1st resolove");
    },
    function () {
        console.log("1st reject");
    }
).then(function () {
    console.log("2nd resolve");
    },
    function () {
        console.log("2nd reject");
    }
);

1) Why does it log -

1st reject
2nd resolve

instead of -

1st reject
2nd reject

?

2) What do I have to change to make it log -

1st reject
2nd reject

?

var myAppModule = angular.module('myApp', []).
		controller('myCtrl',function($scope,$q){
			var deferred = $q.defer();
			//deferred.resolve();
			deferred.reject();
			deferred.promise.then(function () {
					console.log("1st resolove");
				},
				function () {
					console.log("1st reject");
				}
			).then(function () {
				console.log("2nd resolve");
				},
				function () {
					console.log("2nd reject");
				}
			);
		});

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.20/angular.min.js"></script>
<div ng-app="myApp">
		<div ng-controller="myCtrl"></div>
	</div>	

解决方案

You missed the next line of the docs:

This method returns a new promise which is resolved or rejected via the return value of the successCallback, errorCallback.

If you want the next chain to be the result of the same promise for whatever reason, return deferred.promise from inside your handler.

// This will always trigger the reject callback
deferred.promise.then(function () {
        console.log("1st resolove");
        return deferred.promise;
    },
    function () {
        console.log("1st reject");
        return deferred.promise;
    }
)

If you want to return either the success/failed callback, depending on the result of the first, return $q.when() inside your success and return $q.reject() inside your failure:

deferred.promise.then(function () {
        console.log("1st resolove");
        return $q.when();
    },
    function () {
        console.log("1st reject");
        return $q.reject();
    }
).then(function () {
        console.log("2nd resolve");
    },
    function () {
        console.log("2nd reject");
    }
);

Example

这篇关于$q.defer() then of the then的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆