<commit_before>define(function(require) {
    "use strict";

    var UuidHelper = require("../helpers/uuid-helper");

    var _key = {};
    var _getAttributes = UuidHelper.generateUuid();

    /*
        @class Promise

        @classdesc Promise class.
    */

    var STATE = {
        UNFULFILLED: 0,
        FULFILLED: 1,
        REJECTED: 2
    };

    /*
        @constructor Promise

        Constructs a Promise instance.
    */
    var Promise = function(func) {
        //Private instance members
        var attributes = {
            state: STATE.UNFULFILLED,
            queue: [],
            value: null
        };

        //Public instance members
        //(None)

        //Special instance method for private member access
        this[_getAttributes] = function(key) {
            if (key === _key) {
                return attributes;
            }
        };

        //Other steps
        var me = this;

        if (func) {
            func(function(value) {
                me.resolve(value);
            }, function(error) {
                me.transition(STATE.REJECTED, error);
            });
        }

        //Return instance
        return this;
    };

    //Public prototype members
    Promise.prototype = (function(prototype) {
        /*
            @function run

            Processes the desired promise's queue.

            @param {object} promise     The desired promise
        */
        var run = function(promise) {
            if (promise.get("state") === STATE.UNFULFILLED) {
                return;
            }

            setTimeout(function() {
                var queue = promise.get("queue");

                while (queue.length > 0) {
                    var object = queue.shift();
                    var value;

                    try {
                        //jshint -W083
                        value = (promise.get("state") === STATE.FULFILLED ?
                            (object.fulfill || function(x) { return x; }) :
                            (object.reject || function(x) { throw x; }))(promise.get("value"));
                        //jshint +W083
                    }
                    catch (e) {
                        object.promise.transition(STATE.REJECTED, e);
                        continue;
                    }
                    object.promise.resolve(value);
                }
            }, 0);
        };


        /*
            @function get

            Gets the specified attribute.

            @param {string} attribute       The desired attribute

            @return {mixed}
        */
        prototype.get = function(attribute) {
            return this[_getAttributes](_key)[attribute];
        };

        /*
            @function transition

            Transitions the promise to the desired state.

            @param {int} state          The desired state
            @param {mixed} value        The desired value
        */
        prototype.transition = function(state, value) {
            var currentState = this.get("state");

            if (currentState === state || currentState !== STATE.UNFULFILLED || value === undefined ||
                (state !== STATE.FULFILLED && state !== STATE.REJECTED) ||
                arguments.length < 2) {
                return false;
            }

            var attributes = this[_getAttributes](_key);
            attributes.state = state;
            attributes.value = value;

            run(this);
        };

        /*
            @function then

            Returns a new Promise with the desired callbacks.

            @param {function} fulfillCallback       The desired fulfill callback
            @param [{function}] rejectCallback      The desired reject callback

            @return {object}
        */
        prototype.then = function(fulfillCallback, rejectCallback) {
            var promise = new Promise();
            var queue = this.get("queue");<commit_msg>Util.Promise: updated undefined comparison to ensure it is safe.<commit_after>define(function(require) {
    "use strict";

    var UuidHelper = require("../helpers/uuid-helper");

    var _key = {};
    var _getAttributes = UuidHelper.generateUuid();

    /*
        @class Promise

        @classdesc Promise class.
    */

    var STATE = {
        UNFULFILLED: 0,
        FULFILLED: 1,
        REJECTED: 2
    };

    /*
        @constructor Promise

        Constructs a Promise instance.
    */
    var Promise = function(func) {
        //Private instance members
        var attributes = {
            state: STATE.UNFULFILLED,
            queue: [],
            value: null
        };

        //Public instance members
        //(None)

        //Special instance method for private member access
        this[_getAttributes] = function(key) {
            if (key === _key) {
                return attributes;
            }
        };

        //Other steps
        var me = this;

        if (func) {
            func(function(value) {
                me.resolve(value);
            }, function(error) {
                me.transition(STATE.REJECTED, error);
            });
        }

        //Return instance
        return this;
    };

    //Public prototype members
    Promise.prototype = (function(prototype) {
        /*
            @function run

            Processes the desired promise's queue.

            @param {object} promise     The desired promise
        */
        var run = function(promise) {
            if (promise.get("state") === STATE.UNFULFILLED) {
                return;
            }

            setTimeout(function() {
                var queue = promise.get("queue");

                while (queue.length > 0) {
                    var object = queue.shift();
                    var value;

                    try {
                        //jshint -W083
                        value = (promise.get("state") === STATE.FULFILLED ?
                            (object.fulfill || function(x) { return x; }) :
                            (object.reject || function(x) { throw x; }))(promise.get("value"));
                        //jshint +W083
                    }
                    catch (e) {
                        object.promise.transition(STATE.REJECTED, e);
                        continue;
                    }
                    object.promise.resolve(value);
                }
            }, 0);
        };


        /*
            @function get

            Gets the specified attribute.

            @param {string} attribute       The desired attribute

            @return {mixed}
        */
        prototype.get = function(attribute) {
            return this[_getAttributes](_key)[attribute];
        };

        /*
            @function transition

            Transitions the promise to the desired state.

            @param {int} state          The desired state
            @param {mixed} value        The desired value
        */
        prototype.transition = function(state, value) {
            var currentState = this.get("state");

            if (currentState === state || currentState !== STATE.UNFULFILLED || typeof value === "undefined" ||
                (state !== STATE.FULFILLED && state !== STATE.REJECTED) ||
                arguments.length < 2) {
                return false;
            }

            var attributes = this[_getAttributes](_key);
            attributes.state = state;
            attributes.value = value;

            run(this);
        };

        /*
            @function then

            Returns a new Promise with the desired callbacks.

            @param {function} fulfillCallback       The desired fulfill callback
            @param [{function}] rejectCallback      The desired reject callback

            @return {object}
        */
        prototype.then = function(fulfillCallback, rejectCallback) {
            var promise = new Promise();
            var queue = this.get("queue");
{'fraction_non_alphanumeric': 0.10110524993720171, 'fraction_numerical': 0.003014318010550113, 'mean_word_length': 1.2596481271282634, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5892850', 'n_tokens_mistral': 1997, 'n_tokens_neox': 1866, 'n_words': 492}