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);
    });
  };
};

4.63
8
Mae 95 points

                                    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);
})();

4.63 (8 Votes)
0
Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source