js-vault icon indicating copy to clipboard operation
js-vault copied to clipboard

Promisify a function

Open nitish24p opened this issue 5 years ago • 3 comments

Given a function as mentioned below. Write a utility, promisify, where the usage is as explained below

function asyncFunction(a, b, callback) {
  const isError = Math.random() > 0.7;
  setTimeout(() => {
    if (isError) {
      callback(null, new Error("Something went wrong"));
      return;
    }

    callback(a + b, null);
  }, 3000);
}

function promisify(fn) {
  // Implement Code here
}

// Old ussage
asyncFunction(1,2, function(result, err) {
  console.log(result,err)
})


// New Usage
const promisifiedFn = promisify(asyncFunction);
 promisifiedFn(1, 2)
   .then(res => {
     console.log("result", res);
   })
   .catch(console.log);

nitish24p avatar Oct 05 '20 08:10 nitish24p

I am interested to work on this. Can I proceed?

If yes, am I supposed to add the same in src/content/promisify/promisify.mdx ?

pnshiralkar avatar Oct 05 '20 09:10 pnshiralkar

Yea works also add the route in routes.js

nitish24p avatar Oct 05 '20 11:10 nitish24p

Sure

pnshiralkar avatar Oct 05 '20 11:10 pnshiralkar