metalsmith / permalinks

A Metalsmith plugin for permalinks.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Reason behind setImmediate(done)?

petasittek opened this issue · comments

What is the reason of calling the callback function done() right at the beginning instead of the expected place - after the processing is done? Is there any speedup factor or? I find it quite confusing.

See: https://github.com/segmentio/metalsmith-permalinks/blob/16b3be73a18b33a0fab826cb46489d6f3cb9476e/lib/index.js#L35

@petasittek ,
As I've read https://nodejs.org/api/timers.html#timers_setimmediate_callback_args, this takes done callback to the end of the event loop. I'm not sure, but may be it should be placed after all plugin manipulations.

Someone should look into using done() directly instead of setImmediate()

Logrocket has a good article about this: https://blog.logrocket.com/node-js-multithreading-worker-threads-why-they-matter/ (title: The naive solution: Synchronous code-splitting). The article admits "it’s hard to know where to put the setImmediate() to find a good balance.. In the context of this plugin, changing setImmediate(done) to just done() at the end would be clearer and very likely make no difference