"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var Promise_1 = require("@dojo/shim/Promise");
var Deferred = (function () {
    function Deferred() {
        var _this = this;
        this.promise = new Promise_1.default(function (resolve, reject) {
            _this._resolver = resolve;
            _this._rejector = reject;
        });
        this.promise.then(function () { return _this._finalize; }, function () { return _this._finalize; });
    }
    Deferred.prototype.callback = function (callback) {
        var _this = this;
        return this.rejectOnError(function () {
            var args = [];
            for (var _i = 0; _i < arguments.length; _i++) {
                args[_i] = arguments[_i];
            }
            var returnValue = callback.apply(_this, args);
            _this.resolve();
            return returnValue;
        });
    };
    Deferred.prototype.rejectOnError = function (callback) {
        var _this = this;
        return function () {
            var args = [];
            for (var _i = 0; _i < arguments.length; _i++) {
                args[_i] = arguments[_i];
            }
            try {
                return callback.apply(_this, args);
            }
            catch (error) {
                _this.reject(error);
            }
        };
    };
    Deferred.prototype.resolve = function (value) {
        this._resolver(value);
    };
    Deferred.prototype.reject = function (error) {
        this._rejector(error);
    };
    Deferred.prototype._finalize = function () {
        this._resolver = function () { };
        this._rejector = function () { };
    };
    return Deferred;
}());
exports.default = Deferred;
//# sourceMappingURL=Deferred.js.map