Is this return statement in the _.delay function of underscore.js extraneous?

可紊 提交于 2019-12-12 04:19:06

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!