简体   繁体   English

承诺拒绝,然后再进行循环

[英]Promise rejects before finishing for loop

this might be a easy fix, but I am running into a problem with using a loop with a Promise. 这可能是一个简单的修复,但是我在使用Promise循环时遇到了问题。 I am using Angular and Typescript. 我正在使用Angular和Typescript。

As a backstory, I am trying to implement a very simple user authentication login page. 作为背景,我正在尝试实现一个非常简单的用户身份验证登录页面。 I have a Array of User objects, each object containing a username, password, etc. On the login page, when the user inputs their User/Pass, clicks a "Login" button, a promise function fires which loops through the Array to see if there is a match between the Array and the user input. 我有一个用户对象数组,每个对象都包含用户名,密码等。在登录页面上,当用户输入用户名/密码时,单击“登录”按钮,将触发一个promise函数,该函数遍历该数组以查看如果Array和用户输入之间存在匹配项。

Here's some pseudocode that I have written, where usersList is the array of Users and users.username and users.password is the user input. 这是我编写的一些伪代码,其中usersList是Users和users.username的数组users.usernameusers.password是用户输入。

for (var i = 0; i < this.usersList.length; i++) {
    if (this.usersList[i].email === user.username && this.usersList[i].password === user.password) {
        resolve({
            /* code */
        })
    } else {
        reject({
            message: "User/Password not found"
        })
    }

However, this only works for the first object in the list, since the Promise rejects on the first iteration of the for loop. 但是,这仅适用于列表中的第一个对象,因为Promise拒绝了for循环的第一次迭代。 How would I go ahead and fix this so that it only rejects after the loop has gone through every object in the Array? 我将如何解决此问题,使其仅在循环遍历数组中的每个对象后才拒绝?

The obvious problem is you're calling reject as soon as one item has been checked and found to not match the input. 明显的问题是,一旦一项被检查并发现与输入不匹配,您就会立即调用reject The loop does continue, but by that point it's too late to resolve . 循环的确继续进行,但到那时resolve为时已晚。

A better idea might be to filter your users list to see if the item exists, if it does, resolve else reject . 一个更好的主意可能是过滤您的users列表,以查看该项目是否存在,如果存在,则resolve else reject Something like: 就像是:

var foundItems = this.usersList.filter(u => u.username == user.username && u.password == user.password);
if(foundItems.length){
    resolve({
            /* code */
        });
}
else{
    reject({
            message: "User/Password not found"
        });
}

You could also use find with similar effect: 您还可以使用具有类似效果的find

var user = this.usersList.find(u => u.username == user.username && u.password == user.password);
if(user){
    resolve({
            /* code */
        });
}
else{
    reject({
            message: "User/Password not found"
        });
}

You basically fix your code by moving the reject. 您基本上可以通过移动拒绝来修复代码。

for (var i = 0; i < this.usersList.length; i++) {
    if (this.usersList[i].email === user.username && this.usersList[i].password === user.password) {
        resolve({
            /* code */
        })
        return
    }
}
reject({
    message: "User/Password not found"
})

You need to return to break the loop when found and also, if this code runs in a function, to stop the execution of function. 找到该代码后,您需要返回以中断循环;如果此代码在函数中运行,则还需要停止该函数的执行。

Promise reject at first iteration because it did not match the object at index(0) of Array and code inside the else loop executed. Promise在第一次迭代时拒绝,因为它与Array的index(0)处的对象不匹配,并且与else循环内的代码不匹配。 To resolve this problem you should write the code where the promise reject at last iteration if it not resolved in all iteration before the last one. 要解决此问题,您应该编写代码,如果未在最后一个迭代之前的所有迭代中都解决了诺言,则在最后一个迭代中拒绝了诺言。

 for (var i = 0; i < this.usersList.length; i++) {
    if (this.usersList[i].email === user.username && this.usersList[i].password === user.password) {
        resolve({
            /* code */
        })
    } else if(i === this.usersList.length - 1) {
        reject({
            message: "User/Password not found"
        })
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM