首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Promise是解决每个“if”条件,而不是只返回一个条件

Promise是解决每个“if”条件,而不是只返回一个条件
EN

Stack Overflow用户
提问于 2020-09-24 20:49:23
回答 1查看 30关注 0票数 0

我正在尝试返回一个特定的响应,这取决于我获得的一些数据。问题是,每次它都会返回最后一个响应,即使它已经通过了正确的条件:

代码语言:javascript
运行
复制
api.get('/database-manager/request-status', async (lambdaRequest, lambdaResponse, next) => {
        if (connectionPool == null) {
            connectionPool = await mySQLConnectionPool();
        }
        requestId = lambdaRequest.headers["request-id"]
        const res = await new Promise(
            async function (resolve, reject) {
                await getRequests()
                for (let index in requestIds) {
                    console.log(`requestId in header : ${requestId}`)
                    if (requestId == requestIds[index].request_id) {
                        console.log(`id in list : ${requestIds[index].request_id}`)
                        requestStatus = requestIds[index].request_status
                        console.log("am in the first if ")
                        if (requestStatus == 'Finished') {
                            console.log("am in the finished if ")
                            ec2InstanceLink = requestIds[index].instance_link
                            let response = {
                                "statusCode": 200,
                                "headers": {},
                                "isBase64Encoded": false,
                                "body": ec2InstanceLink
                            }
                            resolve(response)
                        } else {
                            console.log("am in the not finished if ")
                            let response = {
                                "statusCode": 200,
                                "headers": {},
                                "isBase64Encoded": false,
                                "body": JSON.stringify({status: requestStatus})
                            }
                            resolve(response)
                        }
                    }
                    console.log("cannot find")
                    let response = {
                        "statusCode": 200,
                        "headers": {},
                        "isBase64Encoded": false,
                        "body": "Request-Id doesn't exist"
                    }
                    resolve(response)
                }
            }
        )
        return res;
    }
)

这是控制台日志,证明即使它处于正确的情况,它也没有返回正确的响应。:

代码语言:javascript
运行
复制
1600950719600   START RequestId: 3436e868-b36f-4d51-88c0-0c6b3d06bbb7 Version: $LATEST
1600950721777   2020-09-24T12:32:01.777Z    3436e868-b36f-4d51-88c0-0c6b3d06bbb7    INFO    requestId in header : 911e8b872dbe7fc8a2821008bf8af87c
1600950721795   2020-09-24T12:32:01.777Z    3436e868-b36f-4d51-88c0-0c6b3d06bbb7    INFO    cannot find
1600950721815   2020-09-24T12:32:01.815Z    3436e868-b36f-4d51-88c0-0c6b3d06bbb7    INFO    requestId in header : 911e8b872dbe7fc8a2821008bf8af87c
1600950721815   2020-09-24T12:32:01.815Z    3436e868-b36f-4d51-88c0-0c6b3d06bbb7    INFO    id in list : 911e8b872dbe7fc8a2821008bf8af87c
1600950721815   2020-09-24T12:32:01.815Z    3436e868-b36f-4d51-88c0-0c6b3d06bbb7    INFO    am in the first if
1600950721815   2020-09-24T12:32:01.815Z    3436e868-b36f-4d51-88c0-0c6b3d06bbb7    INFO    am in the not finished if
1600950721815   2020-09-24T12:32:01.815Z    3436e868-b36f-4d51-88c0-0c6b3d06bbb7    INFO    cannot find
1600950721815   2020-09-24T12:32:01.815Z    3436e868-b36f-4d51-88c0-0c6b3d06bbb7    INFO    requestId in header : 911e8b872dbe7fc8a2821008bf8af87c
1600950721815   2020-09-24T12:32:01.815Z    3436e868-b36f-4d51-88c0-0c6b3d06bbb7    INFO    cannot find
1600950721836   END RequestId: 3436e868-b36f-4d51-88c0-0c6b3d06bbb7
1600950721836   REPORT RequestId: 3436e868-b36f-4d51-88c0-0c6b3d06bbb7  Duration: 2235.83 ms    Billed Duration: 2300 ms    Memory Size: 128 MB Max Memory Used: 94 MB  Init Duration: 529.96 ms
EN

回答 1

Stack Overflow用户

发布于 2020-09-24 21:21:04

问题是您多次调用resolve,但只有第一次有效。

结构简化:

代码语言:javascript
运行
复制
await new Promise(
            async function (resolve, reject) {
                for (...) {
                    if (...) {
                        if (...) {
                            resolve(response)
                        } else {
                            resolve(response)
                        }
                    }
                    resolve(response)
                }
            }
        )

在for循环的每次迭代中都会调用resolve,并且promises只能解析一次。因为代码是同步的,所以它将处理所有的请求ids,但await new Promise的结果将是第一个响应,即Request-Id doesn't exist

这就是为什么你会看到一个错误的响应。要解决此问题,您只需解析promise一次。如何做到这一点取决于您想要实现什么,如果您搜索匹配的responseId,那么只需将最后一个resolve移出for循环即可。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64046810

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档