Using BlueBird promises, I am trying to make getCredentials
into a promise that will work as getCredentials.then(function() { do this and that } );
. Not sure what I am doing wrong, because when I resolve in writeToFile
the promise is not counted as resolved/fullfilled.
function getPassword(user) {
return new Promise(function(resolve, reject) {
read({prompt: "Password: ", silent: true, replace: "*" }, function (er, pass) {
rest.postJson(URL, payload(user, pass))
.on('fail', httpFail)
.on('error', conError)
.on('success', writeToFile);
});
});
}
function getCredentials() {
return new Promise(function(resolve, reject) {
read({prompt: "Username: "}, function (er, user) {
getPassword(user);
});
});
}
function writeToFile(data, response) {
return new Promise(function(resolve, reject) {
tokenFile = 'gulp/util/token-file.json'
token = {
id: data.access.token.id,
expires: data.access.token.expires
}
token = JSON.stringify(token);
fs.writeFile(tokenFile, token, function(err) {
if (err) throw err;
console.log("Token was successfully retrieved and written to " .cyan +
tokenFile .cyan + "." .cyan);
resolve();
});
});
}
module.exports = getCredentials;