동기식 읽기를 성공했다 ㅇㅅㅇ


index.js


// @ts-check

const sub = require('./sub');

async function main() {
await sub();
console.log('끝에 나타나면 성공');
}

main();


sub.js


// @ts-check

const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
const it = rl[Symbol.asyncIterator]();

/**
*
* @returns {Promise<number>}
*/
async function getCircleArea() {
console.log('반지름을 입력하세요');
const r = Number((await it.next()).value);
return new Promise((resolve, reject) => {
resolve(r * r * Math.PI);
});
}

/**
*
* @returns {Promise<number>}
*/
async function getRectArea() {
console.log('가로를 입력하세요');
const width = Number((await it.next()).value);
console.log('세로를 입력하세요');
const height = Number((await it.next()).value);
return new Promise((resolve, reject) => {
resolve(width * height);
});
}

module.exports = async function sub() {
console.log('입력 [원, 사각형]');

const input = (await it.next()).value;
/**
* @typedef {object} Figure
* @property {string} name
* @property {number} area
*/
/** @type {Figure} */
const figure = {
name: '',
area: 0,
};
figure.name = input;

switch (input) {
case '원':
figure.area = await getCircleArea();
break;
case '사각형':
figure.area = await getRectArea();
break;
default:
console.log('지원하지 않는 도형입니다.');
}
console.log(`${figure.name}의 넓이는 ${figure.area}입니다.`);
rl.close();
};