Node's del command - callback not firing

青春壹個敷衍的年華 提交于 2019-11-30 11:24:25
gfpacheco

del isn't a Node's command, it's probably this npm package. If that's the case it doesn't receive a callback as second parameter, instead it returns a promise and you should call .then(done) to get it called after the del finishes.

Update

A better solution is to embrace the Gulp's promise nature:

Change your clean function to:

function clean(path) {
  return del(path); // returns a promise
}

And your clean-styles task to:

gulp.task('clean-styles', function(){
  var files = config.temp + '/**/*.css';
  return clean(files);
});

As of version 2.0, del's API changed to use promises.

Thus to specify callback you should use .then():

del('unicorn.png').then(callback);

In case you need to call it from a gulp task - just return a promise from the task:

gulp.task('clean', function () {
  return del('unicorn.png');
});

Checking the docs for the del package it looks like you're getting mixed up between node's standard callback mechanism and del's, which is using a promise.

You'll want to use the promise API, with .then(done) in order to execute the callback parameter.

Node and javascript in general is currently in a bit of a state of flux for design patterns to handle async code, with most of the browser community and standards folks leaning towards promises, whereas the Node community tends towards the callback style and a library such as async.

With ES6 standardizing promises, I suspect we're going to see more of these kinds of incompatibilities in node as the folks who are passionate about that API start incorporating into node code more and more.

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