递归承诺调用 - 内存范围变量问题

Recursive Promise Call- Memory Scope Variable Issue

本文关键字:范围 变量 问题 内存 承诺 调用 递归      更新时间:2023-09-26

我有这些函数的目的是通过api调用检索令牌。如果用户输入了错误的密码,承诺将拒绝,并在拒绝时再次调用该函数以再次尝试用户。

如果用户第一次输入正确的密码,则没有问题。

但是,如果用户输入了错误的密码并重试...但再次尝试成功,我遇到了内存问题。由于在第二次尝试时对 callApiToken() 的递归调用,承诺已满并调用callApiToken().then(function() { refreshToken(); })file.token = JSON.parse(tokenString);已完成,但在不同的内存范围内。不知道该怎么办。我这样说是因为例程成功运行。但全球var file并没有按应有的方式填充。

首先调用createTokenFile()

var file = {};
function createTokenFile() {
    block = true;
    callApiToken()
        .then(function() { refreshToken(); }) // ON THE SECOND RECURSIVE     
        .catch(function() {                  // RUN refreshToken() IS CALLED
            callApiToken();
        }).finally(function() {
            block = false;
        });
}
function refreshToken() {
    var tokenFileAbsolute = path.join(__dirname, 'token-file.json');
    return fs.readFileAsync(tokenFileAbsolute, {encoding: 'utf-8'})
        .then(function(tokenString) {
            file.token = JSON.parse(tokenString);
        }).catch(function(err) {
            console.log("No token-file.json file found. " .red +
                "Please complete for a new one." .red);
            createTokenFile();
        });
}

使用其他承诺代码进行更新,该代码为实际getCredentials callApiToken()提供解决方案:

注意:fs.writeFileAsync(tokenFile, token)在第二次递归调用时成功完成。

function getPassword(user) {
    return readAsync({prompt: "Password: ", silent: true, replace: "*" })
        .then(function(pass) {
            return postAsync(URL, payload(user[0], pass[0]));
        });
}
function getCredentials() {
    return readAsync({prompt: "Username: "}).then(getPassword);
}
function writeToFile(data, response) {
    tokenFile =  path.join(__dirname, 'token-file.json');
    token = JSON.stringify({
        id: data.access.token.id,
        expires: data.access.token.expires
    });
    return fs.writeFileAsync(tokenFile, token).then(function(err) {
        if (err) throw err;
        console.log("Token was successfully retrieved and written to " .cyan +
            tokenFile .cyan + "." .cyan);
     });
}

没有"内存范围"这样的东西。你只是有一个时间问题!

如果一个动作是异步的,当你想要等待结果时,你总是必须从函数return一个承诺 - 你似乎确实如此。

var file = {};
function createTokenFile() {
    block = true;
    callApiToken()
        .then(function() {
            return refreshToken();
//          ^^^^^^ here
        })
        .catch(function() {
            return callApiToken();
//          ^^^^^^ and here
        }).finally(function() {
            block = false;
        });
}
function refreshToken() {
    var tokenFileAbsolute = path.join(__dirname, 'token-file.json');
    return fs.readFileAsync(tokenFileAbsolute, {encoding: 'utf-8'})
        .then(function(tokenString) {
            file.token = JSON.parse(tokenString);
        }).catch(function(err) {
            console.log("No token-file.json file found. " .red +
                "Please complete for a new one." .red);
            return createTokenFile();
//          ^^^^^^ and here!!!
        });
}

顺便说一句,我的猜测是你的递归是有缺陷的。难道你不想让refreshToken拒绝,createTokenFile从自身内部(而不是第二callApiToken())称呼自己吗?