在 javascript 函数中使用 done 作为参数是什么意思?

Kou*_*uen 1 javascript function node.js gulp

我正在学习 Javascript,似乎将 done 作为函数中的参数是一个难以理解的概念。我想知道它为什么会这样(作为参数完成(我猜是完成的过程信号),如果有一些好书或在线资源可以进一步研究这个概念。 例如,我正在跟随教程和它使用完成作为参数,问题是当我通过 gulp (gulpfile.js) 在节点上运行代码时,使用完成时进程永远不会停止,如果我选择跳过它运行顺利的代码中的完成。我试图跟踪解决问题,我知道问题是作为参数完成的,(我已经多次检查过)。

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


function clean(path, done) {
    log('Cleaning: ' + $.util.colors.blue(path));
    del(path, done).then(function(path) {
        console.log("path=",util.inspect(path,false,null))
        console.log('Deleted Files\/Folders:\n', path.join('\n'));
        console.log('Finishing clean')
    });
}
Run Code Online (Sandbox Code Playgroud)
  • 节点版本:0.12.4
  • npm 版本:2.10.1
  • 吞咽版本:3.9.0

非常感谢您的帮助,将不胜感激。问候。

yom*_*xzo 5

can only explain the concept. what you are trying to achieve is not clear enough.

done is just a non-official standard name for a function (a.k.a callback) that informs the calling function (parent in stacktrace) that a task is completed.

recall that javascript is asynchronous and functions can be passed around as variables.

now, imagine a function startPrinting that has to call printText1, printText2 and printText3 and then output message that process is completed. We have:

function startPrinting() {
    printText1();
    printText2();
    printText3();
    console.log("completed");
}
function printText1() {
    $.get('http://ps-web.cloudapp.net/proxy.php?url=http://pastebin.com/raw.php?i=3uPCavLN', function(response){
        console.log(response)
    });
}
function printText2() {
    $.get('http://ps-web.cloudapp.net/proxy.php?url=http://pastebin.com/raw.php?i=jZjqKgNN', function(response){
        console.log(response)
    });
}
function printText3() {
    $.get('http://ps-web.cloudapp.net/proxy.php?url=http://pastebin.com/raw.php?i=SreCbunb', function(response){
        console.log(response)
    });
}
Run Code Online (Sandbox Code Playgroud)

here, there is no assurance that completed will ALWAYS be printed after all three functions have been executed. since they execute asynchronously.

in order to sort this, javascript ninjas will introduce a done function so that startPrinting will only print completed when all three functions have been executed. Notice how a function is passed to printText1 ... 2 below:

function startPrinting() {
    /* START OF DONE ROUTINE */
    var count = 0;
    var printCompleted = function() {
        count+=1;

        if(count == 3)
            console.log("completed");
    }
    /* END */
    printText1(printCompleted);
    printText2(printCompleted);
    printText3(printCompleted);
}
function printText1(done) {
    $.get('http://ps-web.cloudapp.net/proxy.php?url=http://pastebin.com/raw.php?i=3uPCavLN', function(response){
        console.log(response)
        done();
    });
}
function printText2(done) {
    $.get('http://ps-web.cloudapp.net/proxy.php?url=http://pastebin.com/raw.php?i=jZjqKgNN', function(response){
        console.log(response)
        done();
    });
}
function printText3(done) {
    $.get('http://ps-web.cloudapp.net/proxy.php?url=http://pastebin.com/raw.php?i=SreCbunb', function(response){
        console.log(response)
        done();
    });
}
Run Code Online (Sandbox Code Playgroud)

I hope you are able to apply this principle to better understanding your context.