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

simple-promise

v1.3.4

Published

Simple and fast promise factory for Node and web apps.

Downloads

16

Readme

simple-promise Bower version NPM version

Simple and fast promise factory for Node and web apps.

$ bower install simple-promise
$ npm install simple-promise

API

promise(task)

  • Param task Function Entry point of the promise.

  • Returns Function

  • var say = promise(function (msg, name) {

    task([{args}], done)

    • Param [args] Any Task arguments.

    • Param done Function Call to signal end of task.

    • Returns Any Return value of task.

    • var say = promise(function (msg, name, done) {

      done([{args}])

      • Param [args] Any Done arguments.
      • Returns Any Return value of then contract.*
      • var thenResult = done('abc123');

promise.then(contract)

  • Param contract Function Called on task's done.

  • Returns Any Return value of then contract.

  • say.then(function (async, sync, msg, name) {

    contract([{async}], [sync], {[args]})

    • Param async Any Call argument(s) of task's done.
    • Param sync Any Return value of task.
    • Returns Any Return value of then contract.
    • say.then(function (async1, async2, sync, msg, name) {

promise.error(contract)

  • Param contract Function Called on task error.

  • Returns Any Return value of error contract.

  • say.error(function (err, msg, name) {

    contract(err, {[args]})

    • Param err Error Error object thrown in task.
    • Param [args] Any All task arguments.
    • Returns Any Return value of error contract.
    • say.error(function (err, msg, name) {

Code Samples

Here are some quick code samples to help you get started.

Load the library

Simple-Promise is a CommonJS library, so the require statement can be used for both Node and web applications!

var promise = require('simple-promise');

Creating a new promise

At their core, promises look and behave like a normal function.

var greet = promise(function (name, done) {
    console.log('Hello %s!', name);
    done();
});

Attach a success behavior

Callbacks can be a hassle and quickly create a mess. Tackle the common usage of callbacks with a promise instead. The then function is optional and is called immediately after successful completion of the promise.

greet.then(function (name) {
    console.log('Farewell %s!', name);
});

Attach an error behavior

Sometimes things don't go as expected. Attach an optional error behavior to handle any problems.

greet.error(function (err, name) {
    console.log('%s caused an error!', name);
    console.error('%s : %s', err.name, err.message);
});

Chain your method calls

Each method supports chaining for quick and clean instantiation.

promise(function (done) {
    console.log('First, this happened.');
    done();
}).then(function () {
    console.log('Then, this happened.');
}).error(function () {
    console.error("Hopefully this won't happen to you.");
});

Immediately invoke your promise

You can invoke immediately with the run method or parens.

var go = promise(function (greeting, name) {
    console.log('%s %s!', greeting, name);
});
go.run('Hello', 'World');
// ~ or ~ like this:
go('Hello', 'World');

Collect all return values

Return values are passed along the chain so you can use them however you need.

var result;
promise(function (name, done) {
    // Some async action.
    setTimeout(function () {
        result = done();
    }, 100);
    return 'Hello!';
}).then(function (sync, name) {
    return name + ' says ' + sync;
}).run('Tom');

var result = promise(function (name) {
    throw Error('Hello!');
}).error(function (err, name) {
    return name + ' says ' + err.message;
}).run('Tom');

Both of these blocks will eventually output the same string; result will equal Tom says Hello!


  • See: http://github.com/cobbdb/simple-promise
  • License: MIT