Answers for "node promisify without err"

0

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);
    });
  };
};
Posted by: Guest on August-25-2020
0

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);
})();
Posted by: Guest on August-18-2020

Code answers related to "node promisify without err"

Code answers related to "Javascript"

Browse Popular Code Answers by Language