Skip to content

Commit b89a01c

Browse files
committed
Convert to ES5 syntax
Replace get-methods with ES5 getters. Replace _.extend with Object.defineProperties.
1 parent 0f49a50 commit b89a01c

File tree

3 files changed

+53
-43
lines changed

3 files changed

+53
-43
lines changed

.jshintrc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@
1111
"onevar":true, // Allows only one var statement per function
1212
"smarttabs":true, // Suppresses warnings about mixed tabs and spaces when the latter are used for alignmnent only
1313
"noarg":true, // Prohibits the use of arguments.caller and arguments.callee
14-
"expr":true // Suppresses warnings about the use of expressions in place of assignments or function calls.
14+
"expr":true, // Suppresses warnings about the use of expressions in place of assignments or function calls.
15+
"es5":true // ES5
1516
}

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,12 @@ promise if `result` is a _value_, or cause it to assume `result`'s (future) stat
7676
_promise_ itself. `defer.reject(reason)` will reject the promise with given `reason`.
7777

7878
`deferred.promise` exposes the deferred's underlying
79-
[promise](https://github.com/promises-aplus/promises-spec). Besides the `then` method, this features
80-
a `getState` method (to get the promise's current state) and a `getResult` method (to get the
81-
promise's eventual fulfillment value or rejection reason).
79+
[promise](https://github.com/promises-aplus/promises-spec). Besides the `then` method, it features
80+
a `state` property (exposing the current state) and a `result` property (exposing the eventual
81+
fulfillment value or rejection reason).
8282

8383
Note that the deferred object may be used in place of the underlying promise as it also implements
84-
the `then` / `getState` / `getResult` API, merely delegating to the latter.
84+
`then` and exposes `state` and `result`.
8585

8686

8787
Testing

tillthen.js

Lines changed: 47 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,6 @@
1717

1818
isFunction: function (f) {
1919
return Object.prototype.toString.call(f) === "[object Function]";
20-
},
21-
22-
extend: function (target, source) {
23-
for (var x in source) { if (source.hasOwnProperty(x)) { target[x] = source[x]; } }
24-
return target;
2520
}
2621
},
2722

@@ -106,8 +101,8 @@
106101

107102
// If `x` is a promise, adopt its (future) state
108103
if (x instanceof TillthenPromise) {
109-
if (x.getState() === "fulfilled") { return deferred.fulfill(x.getResult()); }
110-
if (x.getState() === "rejected") { return deferred.reject(x.getResult()); }
104+
if (x.state === "fulfilled") { return deferred.fulfill(x.result); }
105+
if (x.state === "rejected") { return deferred.reject(x.result); }
111106
return x.then(deferred.fulfill, deferred.reject);
112107
}
113108

@@ -162,6 +157,9 @@
162157
// The actual promise. The deferred will derive from this
163158
promise = new TillthenPromise(),
164159

160+
// The deferred to be returned
161+
deferred = null,
162+
165163
// Queue a handler and a dependant deferred for fulfillment. When (and if) the
166164
// promise is fulfilled, the handler will be evaluated on promise's value and the
167165
// result will be used to resolve the dependant deferred
@@ -235,58 +233,69 @@
235233
return promise;
236234
};
237235

238-
// Attach `then`, `getState` and `getResult` methods to the promise:
239-
_.extend(promise, {
236+
// Attach `then` method as well as `state` and `result` getters to the promise:
237+
Object.defineProperties(promise, {
240238

241239
// Access the promise's current or eventual fulfillment value or rejection reason.
242240
// As soon as (if ever) the promise is fulfilled, the `onFulfilled` handler will
243241
// be evaluated on the promise's fulfillment value. Similarly, as soon as (if ever)
244242
// the promise is rejected, the `onRejected` handler will be evaluated on the
245243
// rejection reason. Returns a new promise which will be eventually resolved
246244
// with the value / reason / promise returned by `onFulfilled` or `onRejected`
247-
then: function (onFulfilled, onRejected) {
248-
249-
// Create a new deferred, one which is *dependant* on (and will be resolved
250-
// with) the the value / reason / promise returned by `onFulfilled` or
251-
// `onRejected`
252-
var dependantDeferred = createDeferred();
253-
254-
// Queue `onFulfilled` and `onRejected` for evaluation upon the promise's
255-
// eventual fulfillment or rejection
256-
queueForFulfillment(onFulfilled, dependantDeferred);
257-
queueForRejection(onRejected, dependantDeferred);
258-
259-
// Return the dependant deferred's underlying promise
260-
return dependantDeferred.promise;
245+
then: {
246+
value: function (onFulfilled, onRejected) {
247+
// Create a new deferred, one which is *dependant* on (and will be resolved
248+
// with) the the value / reason / promise returned by `onFulfilled` or
249+
// `onRejected`
250+
var dependantDeferred = createDeferred();
251+
252+
// Queue `onFulfilled` and `onRejected` for evaluation upon the promise's
253+
// eventual fulfillment or rejection
254+
queueForFulfillment(onFulfilled, dependantDeferred);
255+
queueForRejection(onRejected, dependantDeferred);
256+
257+
// Return the dependant deferred's underlying promise
258+
return dependantDeferred.promise;
259+
}
261260
},
262261

263262
// Get the promise's current state ('pending', 'fulfilled' or 'rejected')
264-
getState: function () { return state; },
263+
state: { get: function () { return state; } },
265264

266-
// Get the promise's result (value or reason). Valid on after the promise has been
267-
// either fulfilled or rejected
268-
getResult: function () { return result; }
265+
// Get the promise's result (value or reason). Valid only after the promise has
266+
// either been fulfilled or rejected
267+
result: { get: function () { return result; } }
269268
});
270269

271-
// Derive a deferred from the promise and return it
270+
// Derive a deferred from the promise, attach needed methods and return it
272271
TillthenDeferred.prototype = promise;
273-
return _.extend(new TillthenDeferred(), {
274-
promise: promise,
275-
fulfill: fulfill,
276-
reject: reject,
277-
resolve: function (valueOrPromise) { resolveDeferred(this, valueOrPromise); }
272+
deferred = new TillthenDeferred();
273+
Object.defineProperties(deferred, {
274+
275+
// Get the deferred's underlying promise
276+
promise: { get: function () { return promise; } },
277+
278+
// Fulfill the promise with given value
279+
fulfill: { value: fulfill },
280+
281+
// Reject the promise with given reason
282+
reject: { value: reject },
283+
284+
// Resolve the promise with given `result`: Fulfill it if `result` is a _value_, or
285+
// cause it to assume `result`'s (future) state if it's a _promise_ itself
286+
resolve: { value: function (result) { resolveDeferred(this, result); } }
278287
});
288+
return deferred;
279289
};
280290

281291
// Attach the `defer` / `getVersion` methods to Tillthen and return it
282-
return _.extend(tillthen, {
292+
Object.defineProperties(tillthen, {
283293

284294
// Get a deferred object: A pending promise with `resolve`, `fulfill` and `reject` methods
285-
defer: createDeferred,
295+
defer: { value: createDeferred },
286296

287297
// Get current version of Tillthen
288-
getVersion: function () {
289-
return "0.2.1"; // Keep in sync with package.json
290-
}
298+
version: { get: function () { return "0.2.1"; } }
291299
});
300+
return tillthen;
292301
}));

0 commit comments

Comments
 (0)