问题
This is the code for _.delay
:
// Delays a function for the given number of milliseconds, and then calls
// it with the arguments supplied.
_.delay = function(func, wait) {
var args = slice.call(arguments, 2);
return setTimeout(function() {
return func.apply(null, args);
}, wait);
};
The first return
statement before setTimeout
is definitely useful because it returns the timeoutID
that can be used to cancel the timer. No question there.
However, I do not see how the return
statement within the function passed to setTimeout
can have any effect since setTimeout
is asynchronous. Is that inner return
statement extraneous?
In order to access that asynchronous result, a promise or similar construct would need to be registered.
underscore.js
is a well-polished library that tries to limit its number of bytes; so, it seems strange that such an extraneous statement would be present.
来源:https://stackoverflow.com/questions/45260518/is-this-return-statement-in-the-delay-function-of-underscore-js-extraneous