Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 5a60302

Browse files
vasileorzapetebacondarwin
authored andcommitted
feat($timeout): allow fn to be an optional parameter
Closes #9176 Close #9723
1 parent 034fade commit 5a60302

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

src/ng/timeout.js

+13-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
function $TimeoutProvider() {
55
this.$get = ['$rootScope', '$browser', '$q', '$$q', '$exceptionHandler',
66
function($rootScope, $browser, $q, $$q, $exceptionHandler) {
7+
78
var deferreds = {};
89

910

@@ -16,15 +17,18 @@ function $TimeoutProvider() {
1617
* block and delegates any exceptions to
1718
* {@link ng.$exceptionHandler $exceptionHandler} service.
1819
*
19-
* The return value of registering a timeout function is a promise, which will be resolved when
20-
* the timeout is reached and the timeout function is executed.
20+
* The return value of calling `$timeout` is a promise, which will be resolved when
21+
* the delay has passed and the timeout function, if provided, is executed.
2122
*
2223
* To cancel a timeout request, call `$timeout.cancel(promise)`.
2324
*
2425
* In tests you can use {@link ngMock.$timeout `$timeout.flush()`} to
2526
* synchronously flush the queue of deferred functions.
2627
*
27-
* @param {function()} fn A function, whose execution should be delayed.
28+
* If you only want a promise that will be resolved after some specified delay
29+
* then you can call `$timeout` without the `fn` function.
30+
*
31+
* @param {function()=} fn A function, whose execution should be delayed.
2832
* @param {number=} [delay=0] Delay in milliseconds.
2933
* @param {boolean=} [invokeApply=true] If set to `false` skips model dirty checking, otherwise
3034
* will invoke `fn` within the {@link ng.$rootScope.Scope#$apply $apply} block.
@@ -33,6 +37,12 @@ function $TimeoutProvider() {
3337
*
3438
*/
3539
function timeout(fn, delay, invokeApply) {
40+
if (!isFunction(fn)) {
41+
invokeApply = delay;
42+
delay = fn;
43+
fn = noop;
44+
}
45+
3646
var skipApply = (isDefined(invokeApply) && !invokeApply),
3747
deferred = (skipApply ? $$q : $q).defer(),
3848
promise = deferred.promise,

test/ng/timeoutSpec.js

+26-2
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,25 @@ describe('$timeout', function() {
105105
}));
106106

107107

108+
it('should allow the `fn` parameter to be optional', inject(function($timeout, log) {
109+
110+
$timeout().then(function(value) { log('promise success: ' + value); }, log.fn('promise error'));
111+
expect(log).toEqual([]);
112+
113+
$timeout.flush();
114+
expect(log).toEqual(['promise success: undefined']);
115+
116+
log.reset();
117+
$timeout(1000).then(function(value) { log('promise success: ' + value); }, log.fn('promise error'));
118+
expect(log).toEqual([]);
119+
120+
$timeout.flush(500);
121+
expect(log).toEqual([]);
122+
$timeout.flush(500);
123+
expect(log).toEqual(['promise success: undefined']);
124+
}));
125+
126+
108127
describe('exception handling', function() {
109128

110129
beforeEach(module(function($exceptionHandlerProvider) {
@@ -165,19 +184,24 @@ describe('$timeout', function() {
165184
var task1 = jasmine.createSpy('task1'),
166185
task2 = jasmine.createSpy('task2'),
167186
task3 = jasmine.createSpy('task3'),
168-
promise1, promise3;
187+
task4 = jasmine.createSpy('task4'),
188+
promise1, promise3, promise4;
169189

170190
promise1 = $timeout(task1);
171191
$timeout(task2);
172192
promise3 = $timeout(task3, 333);
193+
promise4 = $timeout(333);
194+
promise3.then(task4);
173195

174-
$timeout.cancel(promise3);
175196
$timeout.cancel(promise1);
197+
$timeout.cancel(promise3);
198+
$timeout.cancel(promise4);
176199
$timeout.flush();
177200

178201
expect(task1).not.toHaveBeenCalled();
179202
expect(task2).toHaveBeenCalledOnce();
180203
expect(task3).not.toHaveBeenCalled();
204+
expect(task4).not.toHaveBeenCalled();
181205
}));
182206

183207

0 commit comments

Comments
 (0)