【发布时间】:2015-03-12 22:49:58
【问题描述】:
我有这些函数的目的是通过 api 调用检索令牌。如果用户输入了错误的密码,promise 将拒绝并且在拒绝时再次调用该函数以给用户另一次尝试。
如果用户第一次输入正确的密码,则没有问题。
但如果用户输入了错误的密码并再次尝试...但再次尝试成功,我遇到了内存问题。由于第二次尝试对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();
});
}
UPDATE 与其他为 callApiToken() 提供解析的承诺代码实际上是 getCredentials:
注意: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);
});
}
【问题讨论】:
-
你能显示
callApiToken()的代码吗?我怀疑您每次都返回相同的承诺,而不是创建一个新的承诺。 -
我认为你还需要将这个:
.then(function() { refreshToken(); })更改为这个:.then(function() { return refreshToken(); }),这样你就可以与refreshToken()中的其他承诺链接 -
block应该做什么??? -
block是当有重复的请求时......我是由url请求触发的......所以用户刷新了很多功能只会运行一次。
标签: javascript node.js promise bluebird