Skip to content

daniel-aranda/promise-me

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

promise-me

Depreciated!

Update: I will depreciate this lib as there is a native one in node. https://nodejs.org/dist/latest-v8.x/docs/api/util.html#util_util_promisify_original

Things in JavaScript have evolutionated a lot in the last years. The backwards compability from JavaScript is probably the best around all the languages, however something good typically came with something not that good.

One of the problems of old JavaScript are callbacks, and specially callback hells. Modern JavaScript looks like this:

  async myClassMethod(){
  
    await this.someWork();
    
    await this.someOtherWork();

    await this.someOtherWork2();

    //congratulations, job completed!
  
  }

Former JavaScript would be:

  myClassMethod(callback){
  
    someWork(function(){
    
      someOtherWork(function(){
      
        someOtherWork2(function(){
        
          //Your ugly job is completed!
          callback();
        
        });
      
      });
      
    });
  
  }

Having the context that modern JavaScript looks nice and because of backwards compabilities we see tons of code with callbacks, what if we make the callbacks nicer, that is why I created promise-me.

Usage, converting one of the most common callback functions, setTimeout:

  
  //reply is a placeholder to capture arguments from your callback (if any)
  let reply = {};

  await PromiseMe.please(setTimeout, reply, 3000);

Comparing code waiting 3000 miliseconds:

  //before waiting
  await PromiseMe.please(setTimeout, {}, 3000);
  //after waiting, I'm happy no more callback hells
  //before waiting
  setTimeout(() => {
    
    //after waiting, I'm the first step of a callback hell
    
  },3000);

Usage on a custom callback function()

  function myCallback(name, callback){
    callback(`The best magician: ` + name);
  }

Typical usage:

  myCallback((newName) => {
    
     //Output: The best magician: Celi 
     console.log(newName);
    
  }, 'Celi');

Promised way:

  let reply = {};

  await PromiseMe.please(myCallback, reply, `celi`);
  
   //Output: The best magician: Celi   
  console.log(reply.arguments[0]);

Installation:

npm install promise-me-async

Running tests:

  npm test

Output from tests:

image

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published