Skip to content

Latest commit

 

History

History
69 lines (63 loc) · 2.7 KB

README.md

File metadata and controls

69 lines (63 loc) · 2.7 KB

promise


ES6-compatible promise library. Promise/A+ implementation.

promise NPM downloads Build Status Coverage Status Dependency Status Bower version node version Sauce Test Status

Sauce Test Status

example

nodejs

var Promise = require('modulex-promise');
readFilePromisified('config.json')
.then(function (text) { // (A)
    var obj = JSON.parse(text);
    console.log(JSON.stringify(obj, null, 4));
})
.catch(function (reason) { // (B)
    // File read error or JSON SyntaxError
    console.error('An error occurred', reason);
});

standalone

<script src="build/promise-standalone-debug.js"></script>
<script>
    (function (Promise) {
        function httpGet(url) {
            return new Promise(
                function (resolve, reject) {
                    var request = new XMLHttpRequest();
                    request.onreadystatechange = function () {
                        if (this.status === 200) {
                            // Success
                            resolve(this.response);
                        } else {
                            // Something went wrong (404 etc.)
                            reject(new Error(this.statusText));
                        }
                    }
                    request.onerror = function () {
                        reject(new Error(
                            'XMLHttpRequest Error: '+this.statusText));
                    };
                    request.open('GET', url);
                    request.send();
                });
        }
        httpGet('http://example.com/file.txt')
        .then(
            function (value) {
                console.log('Contents: ' + value);
            },
            function (reason) {
                console.error('Something went wrong', reason);
            });
    })(XPromise);
</script>