No need to write .then
in your promise chains.
The promise is the .then
function itself!
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
let log = console.log.bind(console);
let logError = console.error.bind(console);
That's basically it!
Promise-sugar tries to preserve all other behaviors 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
- Copy
promise-sugar.js
to your project or install it using npm:
npm install promise-sugar --save
- Add
promise-sugar.js
to your app usingimport
(ESM),require
(AMD or CommonJs) or as a script tag.
import sweeten from 'promise-sugar';
const sweeten = require('promise-sugar');
<script src="https://unpkg.com/promise-sugar"></script>
- Make sure there is a
Promise
implementation or get a polyfill like es6-promise.
sweeten.usePromise(require('es6-promise').Promise); // polyfill
Regardless of the Promise
implementation used, all sweeten promises have the following methods:
sweeten(promise)
.catch(onReject) // Promite/A+
.timeout(1000) // reject in 1 sec, not a Promise/A+ method
.finally(callback) // not a Promise/A+ method
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
let deffered = sweeten.defer(); // creates a deffered with a sweeten .promise
sweeten.allValues(obj); // Similar to Promise.all(list), but accepts an object with thenable values
if(sweeten.isThenable(any)) any.then(doStuff);
let waiter = sweeten.wait(123); // setTimeout()
waiter.then(doStuffLater);
waiter.stop(); // don't doStuffLater() (like clearTimeout())
function sum(a,b) { return a + b; }
let 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)
Sweeten promises are just promises and functions (then
s) at the same time:
let 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){/*...*/});