在完成函数本身内的所有操作之前,Node是否可以从函数调用中可靠地返回一个值? [英] Could Node feasibly return a value from a function call before completing all operations within the function itself?

查看:97
本文介绍了在完成函数本身内的所有操作之前,Node是否可以从函数调用中可靠地返回一个值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法理解Node是如何操作它的并行处理和从函数调用返回值的。

供参考:下面的gulp函数只是作为一个例子创建的对于这个问题。



函数是否有可能在之前返回数据流读取大文件语句有已完成处理(大文件已从文件系统中完全读取并且已添加流),还是Node足够聪明以在返回之前完成所有语句?

  function moveFiles(){

var gulp = require('gulp'),
stream = require('merge-stream')();

//读取一个大文件
stream.add(gulp.src('src / large-file.txt')
.pipe(gulp.dest('dest / '))
);

//读取一个小文件
stream.add(gulp.src('src / small-file.txt')
.pipe(gulp.dest('dest / '))
);

return(stream.isEmpty()?null:stream);

$ b


解决方案


在完成函数本身内的所有操作之前,Node是否可以从函数调用返回一个值?

这是一个棘手的问题。答案是否定的,以某种方式返回一个值意味着该函数完成执行,它从堆栈中取回,并且不会再做任何事情 - 除非当然是另一次调用,但重点在于这个特定的调用然而,棘手的部分是它是完成执行的函数,并不意味着它不能安排某些东西否则将来会发生。它会在一分钟内变得更复杂,但首先是一个非常简单的例子。

  function x(){
setTimeout( function(){
console.log('x1'));
},2000);
console.log('x2');
return;
console.log('x3');

$ / code>

在这里当你调用 x()然后它会计划另一个函数在2秒后运行,然后它将打印 x2 ,然后它将返回 - 在这一点这个函数不能再为该调用做其他事情。



这意味着 x3 永远不会被打印,但 x1 最终会被打印出来 - 因为它是另一个在超时触发时会被调用的函数。匿名函数不会被调用,因为 x()函数在返回后可以做任何事情,但是因为它设法在超时返回之前安排它。



现在,函数可以返回一个将在稍后解决的promise,而不是仅仅调度将来发生的事情。例如:

  function y(){
console.log('y1');
return new Promise(function(resolve,reject){
setTimeout(function(){
resolve('message from y()');
},2000);
});
console.log('y2');
}

现在,当您运行时:

  var promise = y(); 

会发生什么 y1 会得到打印,一个新的承诺将被返回并且 y2 将永远不会被打印,因为当前 y()返回并且不能做别的事。但它设法安排一个超时,以便在两秒钟后解决承诺。



您可以使用以下方法观察它:

< pre $ promise.then(function(value){
console.log(value);
});

所以在这个例子中你可以看到,当 y()函数本身返回并且不能做任何事情,将来可以调用其他函数(在本例中为匿名函数)并完成 y()功能已经启动。



所以我希望现在明白为什么这是一个棘手的问题。某种功能在返回后不能做任何事情。但它可能已经安排了一些其他功能,如超时,事件处理程序等,可以在函数返回后执行某些操作。如果函数返回的是一个承诺,那么在调用者准备就绪时,调用者可以很容易地观察它的值。



所有示例都可以通过使用箭头功能,但我想明确说明,这些都是单独的功能,其中一些是命名的,有些是匿名的。



有关更多详细信息,请参阅其中一些答案:




I'm having trouble understanding how Node operates regarding it's parallel processing and returning values from function calls.

FYI: The gulp function below is merely created as an example for this question.

Is it possible that the function could return the stream before the Read a large file statement has finished processing (the large file has been fully read from the file system and the stream has been added), or is Node smart enough to complete all statements before returning?

function moveFiles(){

    var gulp = require('gulp'),
        stream = require('merge-stream')();

    // Read a large file
    stream.add(gulp.src('src/large-file.txt')
        .pipe(gulp.dest('dest/'))
    );

    // Read a small file
    stream.add(gulp.src('src/small-file.txt')
        .pipe(gulp.dest('dest/'))
    );

    return (stream.isEmpty() ? null : stream);

}

解决方案

Could Node feasibly return a value from a function call before completing all operations within the function itself?

This is a tricky question. The answer is no, in a way that returning a value means that the function is finished executing, it's taken back from the stack and it will never do anything again - unless it's invoked another time of course, but the point is that this particular invocation is over.

But the tricky part is that it's the function that's finished executing and it doesn't mean that it couldn't schedule something else to happen in the future. It will get more complicated in a minute but first a very simple example.

function x() {
    setTimeout(function () {
        console.log('x1'));
    }, 2000);
    console.log('x2');
    return;
    console.log('x3');
}

Here when you call x() then it will schedule another function to run after 2 seconds, then it will print x2 and then it will return - at which point this function cannot do anything else ever again for that invocation.

It means that x3 will never get printed, but x1 will eventually get printed - because it's another function that will be called when the timeout fires. The anonymous function will get called not because the x() function can do anything after it returns, but because it managed to schedule the timeout before it returned.

Now, instead of just scheduling things to happen in the future, a function can return a promise that will get resolved some time later. For example:

function y() {
    console.log('y1');
    return new Promise(function (resolve, reject) {
        setTimeout(function () {
            resolve('message from y()');
        }, 2000);
    });
    console.log('y2');
}

Now, when you run:

var promise = y();

what will happen is that y1 will get printed, a new promise will get returned and y2 will never get printed because at that point y() returned and cannot do anything else. But it managed to schedule a timeout that will resolve the promise after two seconds.

You can observe it with:

promise.then(function (value) {
    console.log(value);
});

So with this example you can see that while the y() function itself returned and cannot do anything else, some other (anonymous in this case) function can be called in the future and finish the job that the y() function has initiated.

So I hope now it's clear why it's a tricky question. In a way a function cannot do anything after returning. But it could have scheduled some other functions as timeouts, event handlers etc. that can do something after the functions returns. And if the thing that the function returns is a promise then the caller can easily observe the value in the future when it's ready.

All of the examples could be simplified by using the arrow functions but I wanted to make it explicit that those are all separate functions, some of them are named, some are anonymous.

For more details see some of those answers:

这篇关于在完成函数本身内的所有操作之前,Node是否可以从函数调用中可靠地返回一个值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆