抹桥的博客
Language
Home
Archive
About
GitHub
Language
主题色
250
185 words
1 minutes
learnyounode - Functional JavaScript

fun For some frustrating reason, CMD suddenly stopped working after I completed the third-to-last problem. I couldn’t select any more problems, and this issue occurred on both my work and personal computers. So, I ended up giving up on the last three problems and didn’t have the energy to figure out what they were. I’ll just leave it at that.

During the problem-solving process, there was one important point to note, which involves a very common method in JavaScript. That is, functions delayed by setTimeout will only execute after the preceding functions have completely finished. In other words, it can block the JavaScript process. For example:

function repeat(operation, num) {
  // modify this so it can be interrupted
  if (num <= 0) return;
  operation();
  setTimeout(function () {
    return repeat(operation, --num);
  }, 0);
}
module.exports = repeat;

Operation is a time-consuming function. If the subsequent recursive calls are not delayed using setTimeout, it would result in a situation where the repeat function has already finished executing, but many operation calls within it have not yet completed.

Other than that, there’s nothing much to add. All the code is available here.

This article was published on September 24, 2015 and last updated on September 24, 2015, 3665 days ago. The content may be outdated.

learnyounode - Functional JavaScript
https://blog.kisnows.com/en-US/2015/09/24/learnyounode-functional-javascript/
Author
Kisnows
Published at
2015-09-24
License
CC BY-NC-ND 4.0