Monday 19 March 2018

Promise.all() and Readline problem. Need help.

I've been working with NodeJS a lot lately – I'm creating an assistant to help manage Bitbucket repositories, initiate new projects cloning the main template and stuff like that.So, I created a function to get information from the user. You pass the question as a parameter, the function uses readline to get the information and returns a Promise – resolved with the answer and rejected if nothing was typed.Most of the time I need more than just one information, so I'm using Promise.all() with all the questions I need to ask. The problem is: as long as the user answers the first question, the program just stops. I tried using rl.close() everywhere but it just doesn't work. Although it works if I chain the functions like ask(question).then(ask(question).then(ask(question).then...)).On the actual function I'll be handling errors way better than this, but here's an example:function ask(question) { return new Promise((resolve, reject) => { rl.question(question, (answer) => { if (answer.length === 0) reject() resolve(answer) }) }) } Promise.all([ ask('Username:'), ask('Email:'), ask('Client Key:'), ask('Client Secret:') ]).then((data) => { console.log(data); }).catch(() => { console.log("Something got lost."); }) Also, if it helps, I'm asking a simple YES or NO question before this chain – "You need to setup your Bitbucket account first. Do you feel like doing that now? [Y/n]" – But I've already tested it without this question and it still doesn't work.Can anyone tell me what I'm doing wrong?

Submitted March 19, 2018 at 11:45AM by grasfahrer

No comments:

Post a Comment