JSFiddle - React, Tailwind, and code Playground
HTML
<div ng-app="myApp">
<div ng-controller="myController">
<button ng-click="newDeferred()">New Deferred</button>
<input type="text" ng-model="myValue" placeholder="type the message you want to send" />
<button ng-click="resolvePromise(myValue)">Resolve Promise</button>
<button ng-click="rejectPromise(myValue)">Reject Promise</button>
<button ng-click="updatePromise(myValue)">Update Promise</button>
</div>
</div>
CSS
input[type=text] {
display:block;
width:100%;
}
JavaScript
(function (window) {
function Promise() {
var emptyFunc = function () {};
this.resolve = emptyFunc;
this.reject = emptyFunc;
this.notify = emptyFunc;
}
Promise.prototype.then = function (resolved, rejected, notified) {
this.resolve = resolved;
this.reject = rejected;
this.notify = notified;
};
var Q = function () {
this.promise = new Promise();
this.done = false;
};
Q.defer = function () {
var q = new Q();
return q;
};
Q.prototype.resolve = function (result) {
if (!this.done) {
this.promise.resolve(result);
this.done = true;
}
};
Q.prototype.reject = function (reason) {
if (!this.done) {
this.promise.reject(reason);
this.done = true;
}
};
Q.prototype.notify = function (value) {
if (!this.done) {
this.promise.notify(value);
}
};
window.Q = Q;
})(window);
(function () {
var app = angular.module('myApp', []);
app.controller('myController', ['$q', '$scope', function ($q, $scope) {
$scope.newDeferred = function () {
var deferred = Q.defer();
//var deferred = $q.defer();
var promise = deferred.promise;
$scope.deferred = deferred;
function resolved(result) {
alert('Resolved, result: ' + result);
}
function rejected(reason) {
alert('Rejected, reason: ' + reason);
}
function updated(value) {
alert('Updated, value: ' + value);
}
promise.then(resolved, rejected, updated);
};
$scope.resolvePromise = function (result) {
$scope.deferred.resolve(result);
};
$scope.rejectPromise = function (reason) {
$scope.deferred.reject(reason);
};
$scope.updatePromise =...