Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在javascript函数中使用done作为参数有什么意义?_Javascript_Node.js_Function_Gulp - Fatal编程技术网

在javascript函数中使用done作为参数有什么意义?

在javascript函数中使用done作为参数有什么意义?,javascript,node.js,function,gulp,Javascript,Node.js,Function,Gulp,我正在学习Javascript,似乎函数中的done作为参数是一个很难理解的概念。 我想知道为什么它的行为会像一个参数完成的过程信号那样,我猜,如果网上有一些好书或资源来进一步研究这个概念。 例如,我跟随一个教程,它使用done作为参数,问题是,当我通过gulp gulpfile.js在节点上运行代码时,当使用done时,进程永远不会停止,如果我选择跳过代码中的done,它将平稳运行。我正在努力追踪这个问题,我知道这个问题是作为一个参数完成的,我已经检查过多次了 gulp.task('clean

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

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')
    });
}
节点版本:0.12.4 npm版本:2.10.1 大口喝版本:3.9.0 非常感谢您的帮助,我们将不胜感激。
称呼。

函数是JavaScript中的第一类对象。您可以像传递任何其他值一样传递它们。将它们作为参数传递给另一个函数后,您可以使用参数名称调用它们,或者调用另一个函数并将它们作为参数传递给该函数,或者为它们指定属性,或者将它们转换为字符串,或者执行任何您想对其执行的操作

此函数\u设置\u主体{ document.body.innerHTML=你好,世界; } 此函数\u调用\u a\u callbackim\u a\u回调{ 我是回拨电话; }
这个函数称为回调函数这个函数设置函数体 函数是JavaScript中的第一类对象。您可以像传递任何其他值一样传递它们。将它们作为参数传递给另一个函数后,您可以使用参数名称调用它们,或者调用另一个函数并将它们作为参数传递给该函数,或者为它们指定属性,或者将它们转换为字符串,或者执行任何您想对其执行的操作

此函数\u设置\u主体{ document.body.innerHTML=你好,世界; } 此函数\u调用\u a\u callbackim\u a\u回调{ 我是回拨电话; }
这个函数称为回调函数这个函数设置函数体 看起来您的示例在回调方面完全搞砸了。在您的示例中的某些地方,done被用作回调函数—一个从外部给定的函数,在异步进程中完成所有操作并发出操作结束的信号时被调用。在其他情况下,它看起来被用作执行方法本身提供的参数。在另一个例子中,你在承诺中使用它。无论如何,由于我不熟悉gulp,我只能猜测,但我希望下面的示例能够帮助您解释回调和部分承诺的概念。但是,我建议避免在同一代码中出现缺少回调和承诺的情况,因为这会导致混乱

gulp.task('clean-styles', function(done) {
   console.log(1);
   /* we are in the callback of gulp.task: we give the
    * latter this anonymous function to call when the 
    * setup is ready and it gives us function done to
    * call when we are done and signal the engine any errors
    */
   var files = config.temp + '**/*.css';

   /* this defines the action to take when files are actually deleted */
   var callback = function(err, message) {
       console.log(6);
       console.log(message); // expect: looks good
       // this is provided apparently by gulp and calling it signals the engine that everything is completed
       done(err);
   };

   /* we call this function, but some bits (like deletion 
    * run asynchronously. The function will return quickly, but
    * callback (function) will only be called when files are deleted */
   clean(files, callback);
   /* the execution of gulp.task callback is finished,
    * but files are not yet deleted */
   console.log(4);
});

/* done here is not the same done as above, it is actually
 * the function we supply into the call above, i.e. `callback` */
function clean(path, done) {
    /* the cleanup is starting */
    console.log(2);
    /* del is scheduled. it returns a `promise` and if
     * we call `then`, then the given anonymous function
     * will be executed when files are deleted. This is
     * where we call the provided function `done` to
     * signal that the job is complete and execute some action */
    del(path).then(function() {
        /* files are deleted and this callback is called */
        console.log(5);
        /* we let the outer caller know by calling `done` which
         * was given to us from outside */
        done(null, "looks good"); // null means no error
    }).catch(function(err) {
        done(err, "looks bad"); // err is given back
    });
    /* the clean method is through, but files not yet deleted */
    console.log(3);
}

看起来您的示例在回调方面完全搞砸了。在您的示例中的某些地方,done被用作回调函数—一个从外部给定的函数,在异步进程中完成所有操作并发出操作结束的信号时被调用。在其他情况下,它看起来被用作执行方法本身提供的参数。在另一个例子中,你在承诺中使用它。无论如何,由于我不熟悉gulp,我只能猜测,但我希望下面的示例能够帮助您解释回调和部分承诺的概念。但是,我建议避免在同一代码中出现缺少回调和承诺的情况,因为这会导致混乱

gulp.task('clean-styles', function(done) {
   console.log(1);
   /* we are in the callback of gulp.task: we give the
    * latter this anonymous function to call when the 
    * setup is ready and it gives us function done to
    * call when we are done and signal the engine any errors
    */
   var files = config.temp + '**/*.css';

   /* this defines the action to take when files are actually deleted */
   var callback = function(err, message) {
       console.log(6);
       console.log(message); // expect: looks good
       // this is provided apparently by gulp and calling it signals the engine that everything is completed
       done(err);
   };

   /* we call this function, but some bits (like deletion 
    * run asynchronously. The function will return quickly, but
    * callback (function) will only be called when files are deleted */
   clean(files, callback);
   /* the execution of gulp.task callback is finished,
    * but files are not yet deleted */
   console.log(4);
});

/* done here is not the same done as above, it is actually
 * the function we supply into the call above, i.e. `callback` */
function clean(path, done) {
    /* the cleanup is starting */
    console.log(2);
    /* del is scheduled. it returns a `promise` and if
     * we call `then`, then the given anonymous function
     * will be executed when files are deleted. This is
     * where we call the provided function `done` to
     * signal that the job is complete and execute some action */
    del(path).then(function() {
        /* files are deleted and this callback is called */
        console.log(5);
        /* we let the outer caller know by calling `done` which
         * was given to us from outside */
        done(null, "looks good"); // null means no error
    }).catch(function(err) {
        done(err, "looks bad"); // err is given back
    });
    /* the clean method is through, but files not yet deleted */
    console.log(3);
}

只能解释这个概念。你想要达到的目标还不够清楚

done只是函数a.k.a回调的非官方标准名称,它通知stacktrace中调用函数的父函数任务已完成

回想一下,javascript是异步的,函数可以作为变量传递

现在,想象一个函数startPrinting必须调用printText1、printText2和printText3,然后输出一条消息,该过程已经完成。我们有:

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)
    });
}
在这里,无法保证在执行所有三个功能后始终打印完成的。因为它们是异步执行的

为了对其进行排序,javascript忍者将引入一个done函数,以便startPrinting仅在所有三个函数都已执行时才打印完成。请注意函数是如何传递给printText1的。。。下文第2段:


我希望你能运用这个原则更好地理解你的背景。

只能解释这个概念。你想要达到的目标还不够清楚

done只是函数a.k.a回调的非官方标准名称,它通知stacktrace中调用函数的父函数任务已完成

回想一下,javascript是异步的,函数可以作为变量传递

现在,想象一个函数startPrinting必须调用printText1、printText2和printText3,然后输出一条消息,该过程已经完成。我们有:

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)
    });
}
在这里,无法保证在完成所有三项功能后始终打印完成的内容 已经被处决了。因为它们是异步执行的

为了对其进行排序,javascript忍者将引入一个done函数,以便startPrinting仅在所有三个函数都已执行时才打印完成。请注意函数是如何传递给printText1的。。。下文第2段:


我希望您能够运用这一原则更好地理解您的上下文。

可能重复:可能重复:感谢昆汀的回答。感谢昆汀的回答。感谢Oleg的回答和使用catch for Exception的演示以及正确使用done的演示。感谢Oleg的回答和演示如何对异常使用catch和正确使用done。感谢yomexzo的回答。非常有用的示例感谢yomexzo的回答。非常有用的示例