promisify (error,data) code example

Example 1: node promisify without err

function promisify(func, callbackPos) {
  return (...args) => {
    return new Promise((resolve) => {
      const cb = (...args) => {
        resolve(args);
      };
      args.splice(callbackPos ? callbackPos : args.length, 0, cb);
      func(...args);
    });
  };
};

Example 2: node promisify without err

function promisify(func, callbackPos) {
  return (...args) => {
    return new Promise((resolve) => {
      const cb = (...args) => {
        resolve(args);
      };
      args.splice(callbackPos ? callbackPos : args.length, 0, cb);
      func(...args);
    });
  };
};

// Example:

// Import readline
const readline = require('readline');
const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout
});

// Promisify rl.question
const asyncQuestion = promisify(rl.question.bind(rl));

(async () => {
  // Call asyncQuestion (Get some input from the user)
  // Here we get all params back in an array
  const input = (await asyncQuestion('Type something: '))[0];
  console.log('You typed: ' + input);
  
  // We can also use this syntax
  [someOtherInput] = await asyncQuestion('Another input: ');
  console.log('You typed: ' + someOtherInput);
})();