promiset is a tiny implementation of the Promises/A+ spec.
It is promise library in JavaScript, small (< 1kb minified / < 0.6kb gzipped) and fast.
First, install promiset with npm.
$ npm install promiset
Then, include promiset in your code file.
var Promise = require('promiset');
Include promiset in your HTML file.
<script src="promiset-browser.js"></script>
This version (and a minified one) can be built with:
$ build/build.js
var promise = Promise.resolve("one");
promise.then(function (value) { console.log(value); });
/* one */
var brokenPromise = Promise.reject(new Error("Could not keep promise."));
brokenPromise.then(null, function (error) { console.error(error.message); });
/* "Could not keep promise." */
You can also use the catch
method if there is no success callback:
brokenPromise.catch(function (error) { console.error(error.message); });
/* "Could not keep promise." */
function promiseLater(something) {
return new Promise(function (resolve, reject) {
setTimeout(function () {
if (something)
resolve(something);
else
reject(new Error("nothing"));
}, 1000);
});
}
promiseLater("something").then(
function (value) { console.log(value); },
function (error) { console.error(error.message); });
/* something */
promiseLater(null).then(
function (value) { console.log(value); },
function (error) { console.error(error.message); });
/* nothing */
Like calling .then, but the fulfillment value could be an array, which is flattened to the formal parameters of the fulfillment handler.
Promise.resolve([1,2]).spread((a, b) => console.log(a, b))
/* 1 2 */
Promise.resolve(1).spread(a => console.log(a))
/* 1 */
var promises = [promiseLater(1), promiseLater(2), promiseLater(3)];
Promise.all(promises).then(function (values) { console.log(values); });
/* [1, 2, 3] */
Execute an array of promises, returns a promise that resolves or rejects as soon as one of the promises in the iterable resolves or rejects, with the value or reason from that promise.
var promises = [promiseLater(1), promiseLater(2), promiseLater(3)];
Promise.race(promises).then(function (values) { console.log(values); });
/* 1 */
Execute an array of tasks which return a promise serially, returns a promise with array contain the value of every resolved promise in the task function, but it will reject if any of the promises is rejected.
var task1 = () => {
return new Promise((resolve, reject) => {
setTimeout(() => {
console.log('task1');
resolve('task1');
}, 2000);
})
}
var task2 = () => {
return new Promise((resolve, reject) => {
setTimeout(() => {
console.log('task2');
resolve('task2');
}, 2000);
})
}
var task3 = () => {
return new Promise((resolve, reject) => {
setTimeout(() => {
console.log('task3');
resolve('task3');
}, 3000);
})
}
Promise.serial([task1, task2, task3])
.then(data => {
console.log('data:', data);
})
.catch(err => {
console.log('err', err);
})
var promises = [task1, task2, task3];
Promise.serial(promises).then(function (values) { console.log(values); });
/* task1
task2
task3
data: ["task1", "task2", "task3"] */