The Readline question()
function does not return a Promise or the result of an arrow function. So you cannot use then()
with it. You can just do
rl.question('Password: ', (password) => { rl.close(); decrypt(password).then(data => { console.log(data); }); });
If you really need to chain with Promise, you can compose your code differently:
new Promise((resolve) => { rl.question('Password: ', (password) => { rl.close(); resolve(password); }); }).then((password) => { return decrypt(password); //returns Promise }).then((data) => { console.log(data); });
You should probably not forget about .catch()
, otherwise any solution will work, the choice should be based on which code will be easier to read.
You can see a couple of additional promise usage patterns
source share