递归承诺调用 - 内存范围变量问题
Recursive Promise Call- Memory Scope Variable Issue
我有这些函数的目的是通过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()
)称呼自己吗?
相关文章:
- ngDialog-弹出窗口未更新范围变量
- AngularJS范围变量Unwatch
- 将外部控制器的范围变量设置为角度
- 对于使用传递的数据计算的局部范围变量,角度绑定在自定义指令中不起作用
- 如何使用EnquireJS使AngularJS范围变量依赖于媒体查询
- 具有范围变量的控制器不工作
- 范围变量返回长度错误
- 将 NodeJS 模块范围变量作为对象访问
- Angularjs 更新 setTimeout 中的范围变量不起作用
- 递归承诺调用 - 内存范围变量问题
- AngularJS:从指令设置范围变量
- Angularjs:访问范围变量数组并计算平均值
- 范围变量更新,但不更新视图
- 从html调用angularjs控制器中的一个函数,但未定义范围变量
- 如何在编辑范围变量时(暂时)避免摘要循环
- 防止Angular范围变量通过引用自动绑定到服务私有成员
- 使用 AngularJS 从指令设置范围变量
- 集合回调忽略范围变量
- 使用forEach修改角度范围变量
- 因果报应测试中未定义的预期范围变量