Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/406.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 在for循环中突破promise块_Javascript_Angularjs_Angular Promise - Fatal编程技术网

Javascript 在for循环中突破promise块

Javascript 在for循环中突破promise块,javascript,angularjs,angular-promise,Javascript,Angularjs,Angular Promise,我有以下代码- this.storeNameValidate = function(stores) { var deferred = $q.defer(); console.log(stores); for (storeIndex in stores) { this.nameValidate(stores[storeIndex].storeName, 3, 10) .then(function() {

我有以下代码-

this.storeNameValidate = function(stores) {
    var deferred = $q.defer();

    console.log(stores);
    for (storeIndex in stores) {
        this.nameValidate(stores[storeIndex].storeName, 3, 10)
            .then(function() {
                console.log("valid store name");
            }, function() {
                console.log("invalid store name");
                deferred.reject("invalid store name");
            })
    }
    return deferred.promise;
}
如果调用nameValidate错误块,我需要中断for循环。我该怎么做


我可以用标记来做类似的事情

this.storeNameValidate = function(stores) {
    var deferred = $q.defer();
    var flag = false;
    console.log(stores);
    for (storeIndex in stores) {
        this.nameValidate(stores[storeIndex].storeName, 3, 10)
            .then(function() {
                console.log("valid store name");
                var flag = true;
            }, function() {
                var flag = false;
                console.log("invalid store name");
                deferred.reject("invalid store name");
            })
            if (!flag)break;
    }
    return deferred.promise;
}
他们有更好的方法吗?

您应该与承诺数组一起使用,并返回组合承诺。如果其中一个内在承诺失败,这个单一的承诺将被拒绝

this.storeNameValidate = function (stores) {

    var promises = Object.keys(stores).map(function(storeIndex) {
        return this.nameValidate(stores[storeIndex].storeName, 3, 10).then(function () {
            console.log("valid store name");
        }, function () {
            return $q.reject("invalid store name");
        });
    }, this);

    return $q.all(promises);

};
另外,不要滥用
$q.defer
您的案例中不需要它。这种重复使用被称为

此外,如果要拒绝的错误消息始终是“无效的存储名称”(不是特定于存储的),并且您实际上不需要对已验证的存储执行其他操作,那么您也可以同时忽略错误和成功回调。然后代码将变得更干净:

this.storeNameValidate = function (stores) {

    var promises = Object.keys(stores).map(function(storeIndex) {
        return this.nameValidate(stores[storeIndex].storeName, 3, 10);
    }, this);

    return $q.all(promises);

};

使用全局变量并在每次迭代中对其进行测试,如果它设置为
true
,则从循环中断:

var isCalled = false;
this.storeNameValidate = function(stores) {
    var deferred = $q.defer();

    console.log(stores);
    for (storeIndex in stores) {
        if(isCalled){break;} // break if the callback invoked
        this.nameValidate(stores[storeIndex].storeName, 3, 10)
            .then(function() {
                console.log("valid store name");
                isCalled = true;
            }, function() {
                console.log("invalid store name");
                deferred.reject("invalid store name");
                isCalled = true;
            })
    }
    return deferred.promise;
}

你误解了正在发生的事情。您的for循环在其他任何事情发生之前退出。它只是设置了所有调用,然后它们都开始异步运行。您应该做的是保留(例如)一个名为“stop”的全局变量,并在调用validate错误时将其设置为“true”。因此,在正常的成功处理程序中,您可以检查stop是否为true,如果为true,则不执行任何其他操作


事实上,有正确的处理方法,你应该研究很多承诺的例子,你会看到他们是如何做的,比如“拒绝”承诺,以及“什么时候都做”、“什么时候做”。您肯定希望通过非常简单的示例清楚地了解正在发生的事情,这将很好地为您服务。

我可以这样做。storeNameValidate=function(stores){var deferred=$q.defer();var flag=false;console.log(stores);for(storeIndex in stores){this.nameValidate(stores[storeIndex].storeName,3,10)。然后(函数(){console.log(“有效存储名”);},函数(){console.log(“无效存储名”);deferred.reject(“无效存储名”);})返回deferred.promise;}这怎么不起作用?@Erik请看更新的问题,附上了我这边的解决方案,但不确定是否是好方法。是的,我也在猜测这种方法,只是想看看是否有人得到了其他东西。