像这样的东西在异步代码中工作吗?

发布于 2024-11-09 01:16:38 字数 523 浏览 0 评论 0原文

regular = 'a string';
enriched = enrichString(regular);
sys.puts(enriched);

function enrichString(str){
    //run str through some regex stuff or other string manipulations
    return str;
}

现在它似乎正在做我希望它做的事情,但我不知道它是否安全。有时这会导致未定义吗?我需要做这样的事情吗:

regular = 'a string';
enriched = enrichString(regular, function(data){sys.puts(data);});

function enrichString(str, cb){
    //run str through some regex stuff or other string manipulations
    cb(str);
}

谢谢您的帮助!

regular = 'a string';
enriched = enrichString(regular);
sys.puts(enriched);

function enrichString(str){
    //run str through some regex stuff or other string manipulations
    return str;
}

Right now it seems to be doing what I hoped it would do but I don't know if its safe. Could this result in undefined sometimes? Do I need to do something like:

regular = 'a string';
enriched = enrichString(regular, function(data){sys.puts(data);});

function enrichString(str, cb){
    //run str through some regex stuff or other string manipulations
    cb(str);
}

Thanks for the help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

甜`诱少女 2024-11-16 01:16:38

不,你应该没问题。计算代码的直接运行本质上不是异步的。当某些操作涉及外部资源(文件 I/O、网络操作、某些操作系统交互等)时,需要回调。

No, you should be fine. Straight runs of computational code are inherently not asynchronous. Callbacks are needed when there's some action involving external resources — file I/O, network operations, some operating system interactions, etc.

絕版丫頭 2024-11-16 01:16:38

如果您进行异步非阻塞调用,则仅需要回调。

var string = "foo",
    new_string = enrich(foo);

doStuff(new_string);

如果 enrich 被阻塞,则安全。例如

function enrich(str) {
    // do regex stuff with str

    // manipulate it

    return str;
}

阻塞,所以这样做是安全的。其中 as

function enrich(str) {
    // get some data from the database.

    // store the string in a file.

    return str;
}

使用非阻塞 IO 并且不安全。您想要做的是这样的:

function enrich(str, cb) {
    // get some data from the database.

    // store the string in a file.

    return cb(str);
}

var string = "foo",
    new_string = enrich(foo, function (str) {
        doStuff(new_string);
    });

请注意

enriched = richString(regular, sys.puts(data));

不起作用,因为您传入了 sys.puts(data ) 作为函数参数(数据也未定义!)

您需要传入一个函数。

You only need callbacks if your making asychronous non-blocking calls.

var string = "foo",
    new_string = enrich(foo);

doStuff(new_string);

Is safe if enrich is blocking. For example

function enrich(str) {
    // do regex stuff with str

    // manipulate it

    return str;
}

is blocking so it's safe to do this. Where as

function enrich(str) {
    // get some data from the database.

    // store the string in a file.

    return str;
}

Uses non blocking IO and is not safe. What you want to do is this :

function enrich(str, cb) {
    // get some data from the database.

    // store the string in a file.

    return cb(str);
}

var string = "foo",
    new_string = enrich(foo, function (str) {
        doStuff(new_string);
    });

Notice that

enriched = enrichString(regular, sys.puts(data));

Does not work because your passing in the return value of sys.puts(data) as your function parameter (data is undefined aswell!)

You need to pass in a function.

抠脚大汉 2024-11-16 01:16:38

只是为了添加 Pointy 的响应,人们有时会使用 process.nextTick (或在浏览器中 setTimeout(fn,0))强制异步行为 - 这会强制当前执行上下文屈服。例如: https://github.com/caolan/async /blob/master/lib/async.js#L408-410

Just to add on Pointy's response, people sometimes force asynchronous behavior with process.nextTick (or in the browser setTimeout(fn,0)) - this forces the current execution context to yield. Ex: https://github.com/caolan/async/blob/master/lib/async.js#L408-410

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文