benkeen / grunt-search

Grunt plugin that searches a list of files and logs all findings in various formats.
MIT License
15 stars 8 forks source link

Support async or Promises in handlers #12

Open binki opened 8 years ago

binki commented 8 years ago

There seems to be no way to access grunt’s this.async() from within onComplete. See this stackoverflow question.

I know that in the case of that question, fs.writeFileSync() is a workaround or using grunt.file.write() is more correct. But the user may want to

  1. Call some other async API for which there is no synchronous alternative.
  2. Have a way for the search task to wait for the completion before continuing (i.e., I know I can start a Promise() in the callback—which keeps node from exiting before the operation completes. But there’s no way to make the launched task wait right now and you might be launching tasks using grunt’s API rather than the CLI and want to know that when the task completes all of the assets generated by the task are fully written to disk).

IMO, all of the callbacks should just have their return value evaluated by Promise.resolve(). This would work with existing synchronous callbacks and magically support Promises whenever callbacks opt in by returning Promises.