Skip to content

Promise syncatctic sugar - no need to write ".then" in your promise chains

Notifications You must be signed in to change notification settings

duzun/promise-sugar

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Promise syntactic sugar

No need to write .then in your promise chains. The promise is the .then function itself!

What it does?

1 ) It allows you to convert this

Promise.resolve(10)
.then(function(n){
    return n / 2
})
.then(function(n){
    return n * 3
})
.then(log) // -> 15
.catch(logError)

into this

sweeten(10)
(function(n){
    return n / 2
})
(function(n){
    return n * 3
})
(log, logError) // -> 15
(null, logError) // .catch

2 ) and this

// Given two existing promises A and B
A
.then(function () { return B; } ) // wait for A and B and return B's value
.then(log) // -> B's value

into this

A(B)
(log) // -> B's value

where

var log      = console.log.bind(console);
var logError = console.error.bind(console);

That's basically it!

Promise-sugar tries to preserve all other behaviours of the Promise library used.

There is another library that implements a similar paradigm - thunks.

Thunks is different from Promise-sugar an more complex (a thunk is not a promise and it has no .catch() method).

You can play with it on jsBin

Install

  • Copy promise-sugar.js to your project or install it using npm:
npm install promise-sugar --save
  • Add promise-sugar.js to your app using require (AMD or CommonJs) or as a script tag.
var sweeten = require('promise-sugar');
  • Make sure there is a Promise implementation or get a polyfill like es6-promise.
sweeten.usePromise(require('es6-promise').Promise); // polyfill

More sugar

Regardless of the Promise implementation used, all sweeten promises have the following methods:

sweeten(promise)
    .catch(onReject)   // Promite/A+
    .finally(callback) // not a Promise/A+

If Promise.prototype.progress is defined, Promise-sugar will preserve it.

Here are some helper method of Promise-sugar:

sweeten.when(value_or_thenable); // creates a sweeten promise
var defered = sweeten.defer();   // creates a defered with a sweeten .promise
sweeten.allValues(obj);          // Similar to Promise.all(list), but accepts an object with thenable values

function sum(a,b) { return a + b; }
var ssum = sweeten.fn(sum); // sweeten version of sum()
ssum(2, Promise.resolve(3))(log); // -> 5


// Promise/A+ sweet equivalents
sweeten.resolve(val)
sweeten.reject(val)
sweeten.race(list)
sweeten.all(list)

Examples

Sweeten promises are just promises and functions (thens) at the same time:

var result = sweeten(fetch('/my/api'))
             (function(res) { return res.json(); })
;

// Now you have a simple function that contains your result
result(log);

// and it is still a promise!
result.catch(logError);

// and can be used as such
Promise.all([result, fetch('my/api/something/else')])
.then(/*...*/);

// or equivalent of the above
sweeten.all([result, fetch('my/api/something/else')])
(/*...*/);

Sweeten promise constructor:

var myStuff = new sweeten(function (resolve, rejext){
    setTimeout(resolve, 100, Math.random());
});

myStuff(function(myNumber){/*...*/}, function(error){/*...*/});

About

Promise syncatctic sugar - no need to write ".then" in your promise chains

Resources

Stars

Watchers

Forks

Packages

No packages published