npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

promiseclass

v1.0.3

Published

Create Promise chain Class

Downloads

191

Readme

PromiseClass

PromiseClass logo

Create your own chain Promise Class easily.

Build Status Coverage Status NPM version License NPM count NPM count

Features

  1. All method will warp to chain Promise
  2. Support constructor define
  3. Support property define
  4. Support sync & async method define
  5. Support addMethod & addPropertie after class define
  6. Support generator & ES7 await
  7. Support async callback & promise & generator mix use
  8. Supoort define generator method, and promise callback support generator function
  9. Support chai

Quick start

Try it:

var PromiseClass = require('promiseclass');
var co = require('co');

var App = PromiseClass.create({

    constructor(str){
        console.log('constructor:',str);
    },

    syncMethod(n){
        console.log('syncMethod called : '+n);
        return 'syncMethod return : '+n;
    },

    asyncMethod1(n, done){
        console.log('asyncMethod1 called : '+n);
        this.asyncMethod2(n).then(function(){
            console.log('asyncMethod1 done : '+n);
            done(null, 'asyncMethod1 return : '+n);
        }).catch(done);
    },

    asyncMethod2(n, done){
        console.log('asyncMethod2 called : '+n);
        setTimeout(function(){
            console.log('asyncMethod2 done : '+n);
            done(null, 'asyncMethod2 return : '+n);
        }, n);
    },

    *generatorMethod1(n){
        console.log('generatorMethod1 called : '+n);
        return yield this.asyncMethod2(n);
    },

    propertie1: 1,
    propertie2: 2

});

// add async method
App.addMethod('asyncMethod3', function(n, done){
    console.log('asyncMethod3 called : '+n);
    setTimeout(function(){
        console.log('asyncMethod3 done : '+n);
        done(null, 'asyncMethod3 return : '+ n);
    }, n);
});
// add generator method
App.addMethod('generatorMethod2', function*(n){
    console.log('generatorMethod2 called : '+n);
    return yield this.asyncMethod2(n);
});
// add propertie
App.addPropertie('propertie3', 3);

// async callback
function async(){
    console.log('================ async start ================');
    var deferred = Promise.defer();
    var app = new App('async');
    app.syncMethod(0, function(error, ret){
        console.log(ret);
        console.log('--------------------------');
        app.asyncMethod1(1, function(error, ret){
            console.log(ret);
            console.log('================ async end ================');
            console.log('');
            deferred.resolve();
        });
    });
    return deferred.promise;
}

// promise
function promise(){
    console.log('================ promise start ================');
    var deferred = Promise.defer();
    var app = new App('promise');
    app.syncMethod(0).then(function(ret){
        console.log(ret);
        console.log('--------------------------');
    }).asyncMethod1(1).then(function(ret){
        console.log(ret);
        console.log('--------------------------');
    }).asyncMethod2(2).then(function(ret){
        console.log(ret);
        console.log('--------------------------');
    }).asyncMethod3(3).then(function(ret){
        console.log(ret);
        console.log('================ promise end ================');
        console.log('');
        deferred.resolve();
    });
    return deferred.promise;
}

// generator
function generator(){
    console.log('================ generator start ================');
    var deferred = Promise.defer();
    co(function *(){
        var app = new App('generator');
        yield app.syncMethod(0).then(function(ret){
            console.log(ret);
        });
        console.log('--------------------------');
        yield app.asyncMethod1(1).then(function(ret){
            console.log(ret);
        });
        console.log('--------------------------');
        yield app.asyncMethod2(2).then(function(ret){
            console.log(ret);
        });
        console.log('--------------------------');
        yield app.asyncMethod3(3).then(function(ret){
            console.log(ret);
        });
        console.log('--------------------------');
        yield app.generatorMethod1(4).then(function*(ret){
            console.log(ret);
            return yield this.asyncMethod1(5);
        }).then(function(ret){
            console.log(ret);
        });
    }).then(function(){
        console.log('done!');
        console.log('================ generator end ================');
        console.log('');
        deferred.resolve();
    });
    return deferred.promise;
}

async().then(function(){
    return promise();
}).then(function(){
    return generator();
});

Try chai

var PromiseClass = require('promiseclass');
var chai = require("chai");
chai.should();
chai.use(PromiseClass.chaiSupportChainPromise);

var App = PromiseClass.create({
    asyncMethod(n, done){
        setTimeout(function(){
            done(null, n);
        }, n);
    },
});

var app = new App();

describe('chai test', function(){
    it('test', function(){
        return app.asyncMethod(100).should.equal(100)
        .asyncMethod(88).should.equal(88);
    });
});

License

PromiseClass is released under the MIT license:

The MIT License

Copyright (c) 2015-2016 Yanis Wang < [email protected] >

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Thanks