Does a gulp task have to return anything?

試著忘記壹切 提交于 2019-11-28 05:43:15
Drew Noakes

If you do not return a stream, then the asynchronous result of each task will not be awaited by its caller, nor any dependent tasks.

For example, when not returning streams:

$ gulp scripts
[21:25:05] Using gulpfile ~/my-project/gulpfile.js
[21:25:05] Starting 'tsc'...
[21:25:05] Finished 'tsc' after 13 ms
[21:25:05] Starting 'scripts'...
[21:25:05] Finished 'scripts' after 10 ms
[21:25:05] Compiling TypeScript files using tsc version 1.0.1.0

Note here that the scripts task depends upon the tsc task. It reports that tsc completes in 13 milliseconds, which is definitely too fast to be reasonably believed. Then the scripts task appears to start and complete, again in a very small period of time. Finally, the actual operation performed by tsc commences. Clearly neither tsc nor scripts waited for the compilation step to complete.

When these tasks return their streams, the output looks rather different:

$ gulp scripts
[21:42:25] Using gulpfile ~/my-project/gulpfile.js
[21:42:25] Starting 'tsc'...
[21:42:25] Compiling TypeScript files using tsc version 1.0.1.0
[21:42:32] Finished 'tsc' after 6.65 s
[21:42:32] Starting 'scripts'...
[21:42:32] Finished 'scripts' after 204 ms

Here the sequence makes sense, and the reported durations meet expectations.

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