pledges

A micro JS library for promises based on the Promises/A+ specification.

Usage no npm install needed!

<script type="module">
  import pledges from 'https://cdn.skypack.dev/pledges';
</script>

README

Pledges

A micro JS library for promises based on the Promises/A+ specification.

Build Status

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.

Documentation

Installation

Reference the raw Github version of release.min.js in your code.

Getting Started

1 Installation

1.1 Browser

Reference the raw Github version of release.min.js in your code.

Pledges is compatible with requireJS and can be used by wrapping your code in the following block:

require(['promise'], function (promise) {
    // Your code.
});

1.2 Node

Pledges is also available as a node package called "pledges". You can install it to your local repository using npm install pledges --save-dev and you can use the library with node by using var promise = require("pledges").promise; in your JavaScript file.

1.3 Versioning

This project is maintained under the semantic versioning guidlines. This means that releases will have the following format <major>.<minor>.<patch>.

  • 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.

promise();

Arguments
None.

Returns
{Object} promise: A structure that can be manipulated like a promise.

3 Methods

3.1 fulfil

Fulfils the promise.

promise().fulfil(value);

Arguments

  • {Object} value: The promised value.

Returns
{Object} promise: The current promise.

3.2 reject

Rejects the promise.

promise().reject(reason);

Arguments

  • {Object} reason: The reason why the promise was rejected.

Returns
{Object} promise: The current promise.

3.3 then

What to do when the state of the promise has changed.

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.4 state

The current state of the promise.

promise().state();

Arguments
None.

Returns
{Object} state: The current state of the promise (either "unfulfilled", "fulfilled", or "rejected").

3.5 restrict

Restricts access to the promise.

promise().restrict();

Arguments
None.

Returns
{Object} promise: A promise that provides access to the then and state methods only.