GithubHelp home page GithubHelp logo

wrappy's Introduction

Wrappy.js

Wrappy.js is a simple utility library that helps wraps your functions and methods in Promises. For those developers who often deal with Promises and Deferreds in a fluent interface, manually making a function or method return a promise quickly becomes tiresome and produces code bloat.

Wrappy simplfies this by creating a Promise proxy around each method of your methods, allowing you to focus on building your features.

Getting Started

Including it on your page

Simply include the wrappy source file on your page. Wrappy has no dependencies on other libraries or frameworks.

<script src="wrappy.js"></script>

Method on a Object as a Promise

To make your method a Promise object, you need to call wrappy.wrapMethod(), with the object and method name:

// Make myObj.myMethod a Promise proxy that replaces myMethod() with a proxy
wrappy.wrapMethod(myObj, 'myMethod');

When invoking your method, Wrappy will append a Promise object as the last argument, that can either resolve or reject:

// Your custom object
var myObj = {
    myMethod: function(name, promise) {
    
        // Simulate an asynchronous operation that completes in 5sec.
        // This could be an ajax call or something else
        setTimeout(function() {
            promise.resolve('My name is ' + name);
        }, 5000);
        
        // Returns immediately, non-blocking
        
    }
};

// You can now invoke myObj.myMethod as usual, but it's now a Promised method
myObj.myMethod('David').then(function(result) {
    console.log(result); // My name is David
});

Function as a Promise

To make a standalone function a Promise object, you need to call wrappy.wrapFunction() with the function to wrap and assign the result back from Wrappy to your original function:

// Make myFunction a Promise proxy that replaces reassigns itself
myFunction = wrappy.wrapFunction(myFunction);

Then call your function as usual:

function myFunction(promise) {
    // do async stuff...
    promise.reject('Some reason why the async operation failed');
}

myFunction().then(function(result) {
    console.log(result); // 'Some reason why the async operation failed'
});

wrappy's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.