使用async/await方法时出现问题,无法处理所有请求。

huangapple go评论64阅读模式
英文:

Problem with async/await method, can't go through all requests

问题

我有一个函数,当我运行它时,它只处理前两个请求,并忽略后两个请求(sessionId 和 getDetails)。
如果我移除 validateUser,例如,那么它就不会忽略 sessionId。

英文:

I have a func, when I run the function it only goes through the first 2 requests and ignore last two requests(sessionId and getDetails)
If I remove validateUser, for example, then it doesn't ignor the sessionId

func requestAllAuthNetwork(username: String, password: String) async throws {
        Task {
            let token =  try await getToken()
            let validateUser =   try await validateUser(username: username, password: password, token: token.requestToken)
            let sessionId =  try await createSession(token: token.requestToken)
            let getDetails =  try await getDetails(sessionId.sessionID)
        }
    }

I try method "withThrowingTaskGroup" but the result was the same.
And some other method but nothing helped me.

func requestAllAuthNetwork(username: String, password: String) async throws {
        try await withThrowingTaskGroup(of: Void.self) { group in
            let token = try await getToken()
            let validateUser =  try await validateUser(username: username, password: password, token: token.requestToken)

            try await group.next()

            group.addTask {

                let sessionId = try await self.createSession(token: token.requestToken)
                let getDetails = try await self.getDetails(sessionId.sessionID)
            }
            try await group.waitForAll()
        }
    }

答案1

得分: 1

您正在忽略您的错误。

func requestAllAuthNetwork(username: String, password: String) async throws {
    //Task { //删除此行
    let token = try await getToken()
    let validateUser = try await validateUser(username: username, password: password, token: token.requestToken) //如果出现错误
    let sessionId = try await createSession(token: token.requestToken) //这一行不会执行,这是有意设计的。
    let getDetails = try await getDetails(sessionId.sessionID)
    // } //删除此行
}

如果函数标记为async,您不需要Task,而且这样漂浮的Task容易引发竞态条件。

然后,在调用requestAllAuthNetwork(username: String, password: String)时,请确保使用do try catch

do {
    try await requestAllAuthNetwork(username: "YourUsername", password: "Your password")
} catch {
    print(error)
}

我非常确定现在控制台会出现错误。

当某事尝试然后引发错误时,该行以下的操作将不会执行。您必须解决这个问题才能使其余的行运行。

英文:

You are ignoring your errors.

func requestAllAuthNetwork(username: String, password: String) async throws {
        //Task { //Remove this
            let token =  try await getToken()
            let validateUser =   try await validateUser(username: username, password: password, token: token.requestToken) //If this throws an error
            let sessionId =  try await createSession(token: token.requestToken) //This line won't run, this is by design.
            let getDetails =  try await getDetails(sessionId.sessionID)
       // } //Remove this
    }

If the function is marked async your don't need Task and a floating Task like that is a perfect source for a race condition.

Then where you call requestAllAuthNetwork(username: String, password: String)

make sure you have a do try catch

do{
    try await requestAllAuthNetwork(username: "YourUsername", password:"Your password")
}catch{
    print(error)
}

I am pretty certain you will get an error in the console now.

When something try and then throw the operations below that line will not get executed. You have to resolve this for the rest of the lines to run.

huangapple
  • 本文由 发表于 2023年2月6日 20:04:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/75361085.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定