JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 10558
  • Score
    100M100P100Q122967F
  • License Apache-2.0

A wrapper to to allow easy synchronous manipulation of Promises

Package Exports

  • promise-chains

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (promise-chains) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

promise-chains

promise-chains is a simple wrapper function for JavaScript Promises which allows them to be easily manipulated using synchronous syntax.

With normal syntax, one might write:

var foo = promise_for_some_timeconsuming_operation;
var baz = foo.then(function(result) {
  return result.bar;
});
// i.e. `baz` is a promise that will resolve with the 'bar' property of the output of foo.

With promise-chains, baz can be expressed as a direct property of foo, making the syntax simpler:

var wrap = require('promise-chains');
var baz = wrap(promise_for_some_timeconsuming_operation).bar;
// `baz` is equivalent to what it was above

This works with function calls, and can be chained:

var cookies_promise = Promise.delay(5000).return('cookies'); // (resolves with 'cookies' after 5 seconds)
wrap(cookies_promise).split('').join('_').toUpperCase().then(console.log);
// --> prints 'C_O_O_K_I_E_S' after 5 seconds

If a wrapped function call returns a promise, the result will also be wrapped.

var example_promise = some_async_operation();
var result = wrap(example_promise).parse_somehow().do_some_other_async_operation().parse_this_response_too().foo;
// `result` is a Promise that resolves with the `foo` property the of result of both operations, parsing etc.

To use/install:

$ npm install promise-chains

Note: promise-chains uses the ES2015 Proxy object, which is currently not included in the Node runtime by default. To use it, you will have to use Node's --harmony-proxies flag. (e.g. instead of using node yourProject.js, use node --harmony-proxies yourProject.js).

Other things to note:

  • If wrap is called on anything other than a Promise, it will just silently return that thing.
  • If a chained Promise tries to access a nonexistent property, the Promise will end up rejecting with a TypeError. (This is analogous to the Cannot read property 'foo' of undefined error that one would encounter when using objects normally.)
  • To "unwrap" a wrapped promise, access its _raw property. i.e. wrap(some_promise)._raw === some_promise