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

Commit 5f941eb

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 5f941eb

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

src/ng/timeout.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,15 @@ function $TimeoutProvider() {
3333
*
3434
*/
3535
function timeout(fn, delay, invokeApply) {
36-
var skipApply = (isDefined(invokeApply) && !invokeApply),
36+
var args = arguments.length > 3 ? sliceArgs(arguments, 3) : [],
37+
skipApply = (isDefined(invokeApply) && !invokeApply),
3738
deferred = (skipApply ? $$q : $q).defer(),
3839
promise = deferred.promise,
3940
timeoutId;
4041

4142
timeoutId = $browser.defer(function() {
4243
try {
43-
deferred.resolve(fn());
44+
deferred.resolve(fn.apply(null, args));
4445
} catch (e) {
4546
deferred.reject(e);
4647
$exceptionHandler(e);

test/ng/timeoutSpec.js

+13
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,19 @@ describe('$timeout', function() {
145145
}));
146146

147147

148+
it('should pass the timeout arguments in the timeout callback',
149+
inject(function($timeout, log) {
150+
var promise1 = $timeout(function(arg) { throw arg; }, 0, true, 'Some Argument');
151+
var promise2 = $timeout(function(arg1, args2) { throw arg1 +' '+ args2; }, 0, true, 'Are Meant', 'To Be Thrown');
152+
153+
promise1.then(log.fn('success'), function(reason) { log('error: ' + reason); });
154+
promise2.then(log.fn('success'), function(reason) { log('error: ' + reason); });
155+
$timeout.flush();
156+
157+
expect(log).toEqual('error: Some Argument; error: Are Meant To Be Thrown');
158+
}));
159+
160+
148161
it('should forget references to relevant deferred even when exception is thrown',
149162
inject(function($timeout, $browser) {
150163
// $browser.defer.cancel is only called on cancel if the deferred object is still referenced

0 commit comments

Comments
 (0)