Node.js:异步回调执行。这是Zalgo吗?

首先,让我解释一下代码的工作原理-请参阅添加的代码中的注释:

// first you define function A and nothing happens:
function A(callback) {
  console.log('A');
  callback();
}

// then you define function B and nothing happens:    
function B() {
  console.log('B')
}

// then you define function C and nothing happens:
function C() {
  console.log('C');
}

// Now you call function A with argument C:
A(C);
// when A runs it prints 'A' and calls C before it returns
// Now the C runs, prints C and returns - back to A
// A Now has nothing more to do and returns

// Now the execution continues and B can be run:
B();
// B runs and prints 'B'

这与任何语言(如Java,C等)完全一样。

现在,第二个示例:

// first you define function A and nothing happens:
function A(callback) {
  console.log('A');
  process.nextTick(() => {
    callback();
  });
}

// then you define function B and nothing happens:
function B() {
  console.log('B')
}

// then you define function C and nothing happens:
function C() {
  console.log('C');
}

// Then you run A with C passed as an argument:
A(C);
// A prints 'A' and schedules running an anonymous function:
// () => { callback(); }
// on the next tick of the event loop, before I/O events are handled
// but after the current code stack is unrolled
// then it returns
// And then B is run:
B();
// B prints 'B' and returns
// Now nothing else is left to do so the next tick of the event loop starts
// There's one function to run, scheduled earlier so it runs.
// This function runs the `callback()` which was `C`
// so C starts, prints 'C' and returns
// The anonymous function has nothing else to do and returns
// There is no more things on the event loop so the program exits

更新资料

感谢Bergi解释了Zalgo的答案是什么。现在,我更好地了解了您的担忧。

这就是我们所说的zalgo吗?谁能提供给我一个实时的例子,这将导致重大故障?

我看过很多这样的代码:

function x(argument, callback) {
    if (!argument) {
        return callback(new Error('Bad argument'));
    }
    runSomeAsyncFunction(argument, (error, result) => {
        if (error) {
            return callback(new Error('Error in async function'));
        }
        callback({data: result});
    });
}

现在,x()如果存在错误的参数,则可以在返回之前立即运行回调,否则可以在x()返回之后运行回调。此代码非常常见。为了测试这些论点,人们可能会争辩说应该抛出一个异常,但让我们暂时忽略一下,可能会有一些更好的操作错误示例被立即知道- 这只是一个简单的示例。

现在,如果它是这样写的:

function x(argument, callback) {
    if (!argument) {
        return process.nextTick(callback, new Error('Bad argument'));
    }
    runSomeAsyncFunction(argument, (error, result) => {
        if (error) {
            return callback(new Error('Error in async function'));
        }
        callback({data: result});
    });
}

这样可以保证回调不会在x()返回之前被调用。

现在,这是否可能导致“重大故障”完全取决于其使用方式。如果运行以下命令:

let a;
x('arg', (err, result) => {
    // assume that 'a' has a value
    console.log(a.value);
});
// assign a value to 'a' here:
a = {value: 10};

那么它将有时版本崩溃x()process.nextTick,绝不会用的版本崩溃x()process.nextTick()

使用 节点-6.0 执行以下操作

function A(callback) {
  console.log('A');
  callback();
}

function B() {
  console.log('B')
}

function C() {
  console.log('C');
}

A(C);
B();


// Result is A,C,B i expected that A,B,C

但是将上面的示例更改为使用process.nextTick()打印A,B,C

function A(callback) {
  console.log('A');
  process.nextTick(() => {
    callback();
  });
}

function B() {
  console.log('B')
}

function C() {
  console.log('C');
}

A(C);
B();

这就是我们所说的zalgo吗?谁能提供给我一个实时的例子,这将导致重大故障?

你可能想看:
分享给朋友: