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

promise-chaining

v0.1.1

Published

Yet another wrapper library for Promise chaining

Downloads

6

Readme

promise-chaining

node npm version npm downloads GitHub release MIT License

Yet another wrapper library for Promise chaining.

Why chaining?

There are many Promise implementations. Not only that, you might use Deferred preferably. And there's a possibility that one of libraries you use returns custom-made thenable object.

Now you need to chain them. Of course, chained functions/objects should wait for previous ones to finish execution. Some thenable implementations might not handle thenable objects passed to resolve() function.

This Chain library supports chaining of those 'immature' Promise implementations.

var promise1 = new Promise(function (resolve1) {
  doAsyncTask(function () {
    var promise2 = new Promise(function (resolve2) {
      doAsyncTask(function () {
        resolve2('2nd task has done')
      })
    })

    resolve1(promise2)
  })
})

promise1.then(function (ret) {
  // This callback should be called after promise2 has resolved
  console.log(ret) // -> should be '2nd task has done'
})

Example

1: Using Chain

var chain = new Chain(promiseGenerator(1))
// -> Task 1: start

if (SOME_CONDITION /* true, for example */) {
  chain.then([promiseGenerator, null, 2])
  // Same with: chain.then(promiseGenerator.bind(null, 2))
}

chain.then(function () {
  return new Promise(function (resolve) {
    console.log('Task 3: start')

    doAsyncTask(function () {
      console.log('Task 3: done')

      resolve(promiseGenerator(4))
    })
  })
})

if (OTHER_CONDITION /* true, for example */) {
  chain.then([thenableGenerator, null, 5])
  // Same with: chain.then(promiseGenerator.bind(null, 5))
}

chain.then(function () {
  console.log('All done')
})
// -> Task 1: done
// -> Task 2: start
// -> Task 2: done
// -> Task 3: start
// -> Task 3: done
// -> Task 4: start
// -> Task 4: done
// -> Task 5: start
// -> Task 5: done
// -> All done

2: Equivalent code using ES2015 Promise

var chain = promiseGenerator(1)
// -> Task 1: start

if (SOME_CONDITION /* true, for example */) {
  chain = chain.then(promiseGenerator.bind(null, 2))
}

chain = chain.then(function () {
  return new Promise(function (resolve) {
    console.log('Task 3: start')

    doAsyncTask(function () {
      console.log('Task 3: done')

      resolve(promiseGenerator(4))
    })
  })
})

if (OTHER_CONDITION /* true, for example */) {
  chain = chain.then(thenableGenerator.bind(null, 5))
}

chain.then(function () {
  console.log('All done')
})
// -> Task 1: done
// -> Task 2: start
// -> Task 2: done
// -> Task 3: start
// -> Task 3: done
// -> Task 4: start
// -> Task 4: done
// -> Task 5: start
// -> Task 5: done
// -> All done

Predefined variables/functions for example codes above

// Example definition
var SOME_CONDITION = true
var OTHER_CONDITION = true

function promiseGenerator(n, arg) {
  // Creates and returns promise object
  return new Promise(function (resolve) {
    console.log('Task ' + n + ': start')

    // Do an asynchronous task here...
    doAsyncTask(function () {
      console.log('Task ' + n + ': done')

      resolve(arg)
    })
  })
}

function thenableGenerator(n, arg) {
  // Creates and returns thenable object
  var resolve

  console.log('Task ' + n + ': start')

  // Do another asynchronous task here...
  doAsyncTask(function () {
    console.log('Task ' + n + ': done')

    resolve(arg)
  })

  return {
    then: function (callback) {
      resolve = callback
    }
  }
}

function doAsyncTask(callback) {
  setTimeout(callback, 100)
}

Installation

$ npm install promise-chaining --save

APIs

Please read test/promise-chaining.spec.js for available APIs and example usages.

Usage

AMD

define(['promise-chaining/src/promise-chaining'], function (Chain) {
  var chain = new Chain()
})

Browser globals

Load script first:

<script src="promise-chaining/src/promise-chaining.js"></script>

yields global class function Chain.

With Chain class:

var chain = new Chain()

Node.js

const Chain = require('promise-chaining')

let chain = new Chain()

Issues

Feel free to submit issues and enhancement requests.

Contributing

Please refer to JavaScript Standard Style and follow the guideline for submitting patches and additions.

I slightly modified the rule for this project... no space after function name because Sublime Text fails to parse function names if there's space after function name:

function name(arg) { ... }

See .eslintrc for detail.

License

promise-chaining is made available under the terms of the MIT license.