A micro JS library for promises based on the Promises/A+ specification.
npm install pledges
License
This work is licensed under a MIT License.
Contributing
Please make contributions by forking the project and creating a pull-request. Other contributions include maintaining the Wiki and issues.
Pledges is compatible with requireJS and can be used by wrapping your code in the following block:
``JavaScript`
require(['promise'], function (promise) {
// Your code.
});
and you can use the library with node by using var promise = require("pledges").promise; in your JavaScript file.$3
This project is maintained under the semantic versioning guidlines. This means that releases will have the following format .
* Breaking backward compatibility bumps the major (and resets the minor and patch).
* New additions without breaking backward compatibility bumps the minor (and resets the patch).
* Bug fixes and misc changes bumps the patch.2 Getting Started
To create a new promise, use the global "promise" function.
`JavaScript
promise();
`Arguments
None.
Returns
{Object} promise: A structure that can be manipulated like a promise.
3 Methods
$3
Fulfils the promise.
`JavaScript
promise().fulfil(value);
`Arguments
* {Object} value: The promised value.
Returns
{Object} promise: The current promise.
$3
Rejects the promise.
`JavaScript
promise().reject(reason);
`Arguments
* {Object} reason: The reason why the promise was rejected.
Returns
{Object} promise: The current promise.
$3
What to do when the state of the promise has changed.
`JavaScript
promise().then(onFulfilment, onRejection);
`Arguments
* {Function} onFulfilment: A function to be called when the promise is fulfilled.
* {Function} onRejection: A function to be called when the promise is rejected.
Returns
{Object} promise: Promises the completion of onFulfilment or onRejection (restricted promise - see restrict method).
$3
The current state of the promise.
`JavaScript
promise().state();
`Arguments
None.
Returns
{Object} state: The current state of the promise (either "unfulfilled", "fulfilled", or "rejected").
$3
Restricts access to the promise.
`JavaScript
promise().restrict();
`Arguments
None.
Returns
{Object} promise: A promise that provides access to the
then and state` methods only.