1function promisify(func, callbackPos) {
2 return (...args) => {
3 return new Promise((resolve) => {
4 const cb = (...args) => {
5 resolve(args);
6 };
7 args.splice(callbackPos ? callbackPos : args.length, 0, cb);
8 func(...args);
9 });
10 };
11};
1function promisify(func, callbackPos) {
2 return (...args) => {
3 return new Promise((resolve) => {
4 const cb = (...args) => {
5 resolve(args);
6 };
7 args.splice(callbackPos ? callbackPos : args.length, 0, cb);
8 func(...args);
9 });
10 };
11};
12
13// Example:
14
15// Import readline
16const readline = require('readline');
17const rl = readline.createInterface({
18 input: process.stdin,
19 output: process.stdout
20});
21
22// Promisify rl.question
23const asyncQuestion = promisify(rl.question.bind(rl));
24
25(async () => {
26 // Call asyncQuestion (Get some input from the user)
27 // Here we get all params back in an array
28 const input = (await asyncQuestion('Type something: '))[0];
29 console.log('You typed: ' + input);
30
31 // We can also use this syntax
32 [someOtherInput] = await asyncQuestion('Another input: ');
33 console.log('You typed: ' + someOtherInput);
34})();