-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathquestionModule.js
More file actions
38 lines (30 loc) · 1.35 KB
/
Copy pathquestionModule.js
File metadata and controls
38 lines (30 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// Destructure events to use EventEmitter
const {EventEmitter} = require('events')
// Idea is to have a variable that can hold our questions say array
const readline = require('readline');
// Initialize the interface readline with std in and out for I/O
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
})
// collectAnswers function exported using module.exports: It takes in questions array and done as callback
module.exports = (questions,done) => {
const answers = [] // answers array to store user response
const [q1] = questions // array destructuring, stores questions[0] in var q1
// Instance of emitter
const emitter = new EventEmitter();
// questionAnswered function that has answer as its parameter.
const questionAnswered = answer => {
// Before pushing answer call emitter.emit()
emitter.emit("answer",answer);
answers.push(answer) // push the answer to answers array defined above
if(answers.length < questions.length){ // ask the next question, if 1 question answered then answer.length = 1, so we can ask second question i.e. index 1
rl.question(questions[answers.length],questionAnswered)
}else{
emitter.emit("complete",answers)
done(answers)
}
}
rl.question(q1, questionAnswered)
return emitter;
}