Skip to content

wwiechorek/vanillaJS-promises

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

vanillaJS-promises

Simple class to make promises using pure JavaScript.

Usage

function myFunction(fireWhen){
  // Start the promises class
  var myPromise = new Promise();
  
  // Set which function should do the callback
  if (fireWhen === 'done')
    myPromise.resolve("Fork me, I'm done!");
  else
    myPromise.reject("Nooooooooooo!");
  
  // Return the functions for done() and fail()
  return myPromise;
}

// This example will work only with the done function
myFunction('done')
  .done(function(message){ alert(message); })
  .fail(function(message){ alert(message); });
  
// This example will work only with the fail function
myFunction('ehhh...')
  .done(function(message){ alert(message); })
  .fail(function(message){ alert(message); });

Usage with "when" method

function myFunction(fireWhen){
  // Start the promises class
  var myPromise = new Promise();

  // Set which function should do the callback
  if (fireWhen === 'done')
    myPromise.callState('done', "Fork me, I'm done!");
  else
    myPromise.callState('fail', "Nooooooooooo!");

  // Return the functions for done() and fail()
  return myPromise;
}

// This example will work only with the done function
myFunction('done')
  .when('done', function(message){ alert(message); })
  .when('fail', function(message){ alert(message); });

// This example will work only with the fail function
myFunction('fail')
  .when('done', function(message){ alert(message); })
  .when('fail', function(message){ alert(message); });

TODO

  • Make it work
  • Make it work with multiple callbacks
  • Make it work considering the function return

License

MIT

About

Simple class to make promises using pure JavaScript.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors