Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript AngularJS$q服务响应顺序与异常处理_Javascript_Angularjs_Node.js_Promise_Q - Fatal编程技术网

Javascript AngularJS$q服务响应顺序与异常处理

Javascript AngularJS$q服务响应顺序与异常处理,javascript,angularjs,node.js,promise,q,Javascript,Angularjs,Node.js,Promise,Q,我有一个函数,它使用for循环的承诺为异步调用提供顺序响应,但当我从代码中得到异常时,循环中断,但即使在从函数中抛出异常后,我仍希望继续我的循环 我的异步函数是 function asyncFun(a) { var q = $q.defer(); setTimeout(function(){ if(a == 4) throw new Error('custom error'); q.resolve(a); }, 1000);

我有一个函数,它使用
for
循环的承诺为异步调用提供顺序响应,但当我从代码中得到异常时,循环中断,但即使在从函数中抛出异常后,我仍希望继续我的循环

我的异步函数是

function asyncFun(a) { 
    var q = $q.defer(); 
    setTimeout(function(){
        if(a == 4) throw new Error('custom error'); 
        q.resolve(a);
    }, 1000); 
    return q.promise; 
}
function getData() {
    var chain = $q.when();
    for (var i = 0; i < 10; i++) {
        (function(i) {
            chain = chain.then(function() {
                return asyncFun(i).then(function(res) {
                    console.log(res);
                }).catch(function(ex) {
                    throw ex;
                });
            }).catch(function(ex) { throw ex });
        })(i);
    };
    return chain
}
链函数是

function asyncFun(a) { 
    var q = $q.defer(); 
    setTimeout(function(){
        if(a == 4) throw new Error('custom error'); 
        q.resolve(a);
    }, 1000); 
    return q.promise; 
}
function getData() {
    var chain = $q.when();
    for (var i = 0; i < 10; i++) {
        (function(i) {
            chain = chain.then(function() {
                return asyncFun(i).then(function(res) {
                    console.log(res);
                }).catch(function(ex) {
                    throw ex;
                });
            }).catch(function(ex) { throw ex });
        })(i);
    };
    return chain
}
函数getData(){ var chain=$q.when(); 对于(变量i=0;i<10;i++){ (职能(一){ chain=chain.then(函数(){ 返回asyncFun(i).then(函数(res){ 控制台日志(res); }).catch(函数(ex){ 掷骰子; }); }).catch(函数(ex){throw ex}); })(i) ); }; 返回链 } 当我调用
getData()
在抛出错误后停止循环,但我想继续对所有10个条目执行
for
循环


如有任何帮助,我们将不胜感激。

正如我在评论中所说,这些错误可能会被视为特殊值,因此您可以在链承诺后执行特殊行为

请尝试以下代码:

function getData() {
    var chain = $q.when();
    for (var i = 0; i < 10; i++) {
        (function(i) {
            chain = chain.then(function() {
                return asyncFun(i).then(function(res) {
                    console.log(res)
                }).catch(function(ex) {
                    console.log(ex); // do not throw error but handle the error
                });
            }).catch(function(ex) { throw ex });
        })(i);
    };
    return chain
}
函数getData(){ var chain=$q.when(); 对于(变量i=0;i<10;i++){ (职能(一){ chain=chain.then(函数(){ 返回asyncFun(i).then(函数(res){ console.log(res) }).catch(函数(ex){ console.log(ex);//不要抛出错误,而是处理错误 }); }).catch(函数(ex){throw ex}); })(i) ); }; 返回链 }
您将如何处理错误?当您继续执行循环时,您的承诺不可能被拒绝。也许您应该将该错误视为一个特殊值,并像解析正常值一样解析它。My
asyncFun
是在某些情况下使用throw error预定义的,这就是为什么我需要在
getData
中进行更改以完成循环的原因。为什么在不理解问题的情况下否决此问题我没有否决此问题。也许是另一个人。。。