栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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 pre 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()



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/448866.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号