Node.js异步功能*定义* [英] Node.js Asynchronous Function *Definition*

查看:105
本文介绍了Node.js异步功能*定义*的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请,只是想清除我的脑袋...

Please, just to clear something up in my head...

我习惯于在库中使用异步函数编写代码,但是我该如何编写自己的代码呢?

I am used to writing using asynchronous functions in libraries, but how do I write my own?

为了说明我的问题,我组成了一个名为"MadMathz"的模块

To illustrate my question I have made up a module called 'MadMathz'

我知道以下是异步函数的示例用法:

I am aware that the following is an example usage of an asynchronous function :

//load module
var mM = require('./MadMathz');

//perform a function
mM.async_function_addthree(12, function(result) {
  console.log(result)
});

//do something straight afterwards
console.log('Logging this straight after instead of waiting for computation');

我知道该函数的第二个参数是回调,但是如何定义诸如"async_function_addthree"之类的函数?假设出于本示例的目的,async_function_addthree仅将3加到第一个参数上.

I'm aware that the second argument to the function is the callback, but how is a function such as 'async_function_addthree' defined? Let's say for the purpose of this example that async_function_addthree just adds 3 to the first argument.

我只是在试图写出自己的想法而感到困惑.我完全走错了轨道吗?我感觉我已经接近使用node.js到达那里,但这需要解决.

I just confused myself so much trying to write what I thought it might be. Am I totally on the wrong track? I feel like I am close to getting there with node.js but this needs clearing up.

请解释,而不是在可能的情况下将我链接到某个地方.我确信其他人将从这个问题中受益.

Please explain rather than linking me somewhere if possible. I'm sure others will benefit from this question.

推荐答案

实际上,创建"异步"函数没有任何进一步的描述或要求,这听起来有点怪异.通常,用法是避免长时间的 blocking 时段.因此,如果我们有一个触发数据库查询的函数,并且想在完成后调用另一个函数,则在完成工作后,该函数应被称为 ,而原始函数会立即返回控制权-流回 Node (ECMAscript).

Actually it sounds a little weird to create a "asynchronous" function without any further description or requirement. Usually, the usage is about avoiding long blocking periods. So if we have a function which triggers a database-query and we want to call another function when this is completed, that function should get called after the job is done and the original function instantely returns the control-flow back to Node (ECMAscript).

所以,如果我们有一个类似

So if we have a construct like

function someJob( callback ) {
    // heavy work
    callback();
}

现在,此代码仍然非常同步地运行.为了使其处于 async 状态,我们可以调用节点的process.nextTick方法

Now, this code still runs very synchronous. To bring that in an async state, we can invoke node's process.nextTick method

function someJob( callback ) {
    // heavy work
    process.nextTick( callback );
}

现在发生的事情是,Node将在以后的事件循环中运行该 callback 方法(并且它也会做得有些智能). Eventho nextTick声称比setTimeout更有效,它的处理几乎相同(从ECMAscript编码器的角度来看).因此,在浏览器中,我们可以像

What happens now is, that Node will execute that callback method in a later run through its eventloop (and it does that somewhat intelligent too). Eventho nextTick claims to be much more efficient than setTimeout, its pretty much the same deal (from the ECMAscript coders perspective). So in a browser we could go like

function someJob( callback ) {
    // heavy work
    setTimeout( callback, 0 );
}

如果说上述示例方法根本没有多大意义,那将是正确的,因为 async 状态发生在繁重的工作之后 .好吧,是的.实际上,您需要将较重的部分分解为较小的部分,并使用与.nextTick()相同的想法来使其真正有效.

You would be correct if saying that the above example method doesn't make much sense at all, because the async state happens after the heavy work. Well, true. Infact, you would need to break down the heavy part into smaller peaces and make use of the same idea using .nextTick() to make this really efficient.

那也是我一开始感到困惑的原因,实际上每个任务都已经为您提供了回调的可能性.

Thats also the reason for my confusion on the beginning, actually every task already offers callback possibilities for you.

这篇关于Node.js异步功能*定义*的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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