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

Commit a73988e

Browse files
committed
feat($timeout): pass arguments in callback
setTimeout allows you to pass arguments into the callback function in the …3rd argument. Since that’s taken I added it to the …4th mdn.io/setTimeout#Syntax
1 parent a01ce6b commit a73988e

File tree

2 files changed

+54
-11
lines changed

2 files changed

+54
-11
lines changed

src/ng/timeout.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,21 @@ function $TimeoutProvider() {
2828
* @param {number=} [delay=0] Delay in milliseconds.
2929
* @param {boolean=} [invokeApply=true] If set to `false` skips model dirty checking, otherwise
3030
* will invoke `fn` within the {@link ng.$rootScope.Scope#$apply $apply} block.
31+
* @param {...*=} Pass additional parameters to the executed function.
3132
* @returns {Promise} Promise that will be resolved when the timeout is reached. The value this
3233
* promise will be resolved with is the return value of the `fn` function.
3334
*
3435
*/
3536
function timeout(fn, delay, invokeApply) {
36-
var skipApply = (isDefined(invokeApply) && !invokeApply),
37+
var args = sliceArgs(arguments, 3),
38+
skipApply = (isDefined(invokeApply) && !invokeApply),
3739
deferred = (skipApply ? $$q : $q).defer(),
3840
promise = deferred.promise,
3941
timeoutId;
4042

4143
timeoutId = $browser.defer(function() {
4244
try {
43-
deferred.resolve(fn());
45+
deferred.resolve(fn.apply(null, args));
4446
} catch (e) {
4547
deferred.reject(e);
4648
$exceptionHandler(e);

test/ng/timeoutSpec.js

+50-9
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,16 @@ describe('$timeout', function() {
2222
it('should call $apply after each callback is executed', inject(function($timeout, $rootScope) {
2323
var applySpy = spyOn($rootScope, '$apply').andCallThrough();
2424

25-
$timeout(function() {});
25+
$timeout(noop);
2626
expect(applySpy).not.toHaveBeenCalled();
2727

2828
$timeout.flush();
2929
expect(applySpy).toHaveBeenCalledOnce();
3030

3131
applySpy.reset();
3232

33-
$timeout(function() {});
34-
$timeout(function() {});
33+
$timeout(noop);
34+
$timeout(noop);
3535
$timeout.flush();
3636
expect(applySpy.callCount).toBe(2);
3737
}));
@@ -40,7 +40,7 @@ describe('$timeout', function() {
4040
it('should NOT call $apply if skipApply is set to true', inject(function($timeout, $rootScope) {
4141
var applySpy = spyOn($rootScope, '$apply').andCallThrough();
4242

43-
$timeout(function() {}, 12, false);
43+
$timeout(noop, 12, false);
4444
expect(applySpy).not.toHaveBeenCalled();
4545

4646
$timeout.flush();
@@ -89,8 +89,8 @@ describe('$timeout', function() {
8989
// $browser.defer.cancel is only called on cancel if the deferred object is still referenced
9090
var cancelSpy = spyOn($browser.defer, 'cancel').andCallThrough();
9191

92-
var promise1 = $timeout(function() {}, 0, false);
93-
var promise2 = $timeout(function() {}, 100, false);
92+
var promise1 = $timeout(noop, 0, false);
93+
var promise2 = $timeout(noop, 100, false);
9494
expect(cancelSpy).not.toHaveBeenCalled();
9595

9696
$timeout.flush(0);
@@ -105,6 +105,28 @@ describe('$timeout', function() {
105105
}));
106106

107107

108+
it('should pass the timeout arguments in the timeout callback',
109+
inject(function($timeout, $browser) {
110+
var task1 = jasmine.createSpy('Nappa'),
111+
task2 = jasmine.createSpy('Vegeta');
112+
113+
$timeout(task1, 9000, true, 'What does', 'the timeout', 'say about', 'its delay level');
114+
expect($browser.deferredFns.length).toBe(1);
115+
116+
$timeout(task2, 9001, false, 'It\'s', 'over', 9000);
117+
expect($browser.deferredFns.length).toBe(2);
118+
119+
$timeout.flush(0);
120+
expect(task1).not.toHaveBeenCalled();
121+
122+
$timeout.flush(9000);
123+
expect(task1).toHaveBeenCalledWith('What does', 'the timeout', 'say about', 'its delay level');
124+
125+
$timeout.flush(1);
126+
expect(task2).toHaveBeenCalledWith('It\'s', 'over', 9000);
127+
}));
128+
129+
108130
describe('exception handling', function() {
109131

110132
beforeEach(module(function($exceptionHandlerProvider) {
@@ -114,7 +136,7 @@ describe('$timeout', function() {
114136

115137
it('should delegate exception to the $exceptionHandler service', inject(
116138
function($timeout, $exceptionHandler) {
117-
$timeout(function() {throw "Test Error";});
139+
$timeout(function() { throw "Test Error"; });
118140
expect($exceptionHandler.errors).toEqual([]);
119141

120142
$timeout.flush();
@@ -126,7 +148,7 @@ describe('$timeout', function() {
126148
function($timeout, $rootScope) {
127149
var applySpy = spyOn($rootScope, '$apply').andCallThrough();
128150

129-
$timeout(function() {throw "Test Error";});
151+
$timeout(function() { throw "Test Error"; });
130152
expect(applySpy).not.toHaveBeenCalled();
131153

132154
$timeout.flush();
@@ -145,6 +167,25 @@ describe('$timeout', function() {
145167
}));
146168

147169

170+
it('should pass the timeout arguments in the timeout callback even if an exception is thrown',
171+
inject(function($timeout, log) {
172+
var promise1 = $timeout(function(arg) { throw arg; }, 9000, true, 'Some Arguments');
173+
var promise2 = $timeout(function(arg1, args2) { throw arg1 + ' ' + args2; }, 9001, false, 'Are Meant', 'To Be Thrown');
174+
175+
promise1.then(log.fn('success'), function(reason) { log('error: ' + reason); });
176+
promise2.then(log.fn('success'), function(reason) { log('error: ' + reason); });
177+
178+
$timeout.flush(0);
179+
expect(log).toEqual('');
180+
181+
$timeout.flush(9000);
182+
expect(log).toEqual('error: Some Arguments');
183+
184+
$timeout.flush(1);
185+
expect(log).toEqual('error: Some Arguments; error: Are Meant To Be Thrown');
186+
}));
187+
188+
148189
it('should forget references to relevant deferred even when exception is thrown',
149190
inject(function($timeout, $browser) {
150191
// $browser.defer.cancel is only called on cancel if the deferred object is still referenced
@@ -218,7 +259,7 @@ describe('$timeout', function() {
218259
// $browser.defer.cancel is only called on cancel if the deferred object is still referenced
219260
var cancelSpy = spyOn($browser.defer, 'cancel').andCallThrough();
220261

221-
var promise = $timeout(function() {}, 0, false);
262+
var promise = $timeout(noop, 0, false);
222263

223264
expect(cancelSpy).not.toHaveBeenCalled();
224265
$timeout.cancel(promise);

0 commit comments

Comments
 (0)