了解异步code通俗地说 [英] Understanding Asynchronous Code in Layman's terms

查看:124
本文介绍了了解异步code通俗地说的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解异步岬基本的事情:事情不按顺序执行。我知道有一些事很强大......据说。但对我的生活,我不能换我的头周围的code。让我们来看看异步Node.js的code,我已经写了...但并没有真正得到的。

I understand the basic thing about asynchronous-ness: things don't execute sequentially. And I understand there is something very powerful about that... allegedly. But for the life of me I can't wrap my head around the code. Let's take a look at async Node.JS code that I HAVE WRITTEN...but don't truly get.

function newuser(response, postData) {
    console.log("Request handler 'newuser' was called.");
    var body = '<html>' + 
        '<head>' +
        '<meta http-equiv="Content-Type" content="text/html; ' +
        'charset=UTF-8" />' +
        '</head>' +
        '<body>' +
        '<form action=" /thanks" method="post">' +
        '<h1> First Name </h1>' +
        '<textarea name="text" rows="1" cols="20"></textarea>' +
        '<h1> Last Name </h1>' +
        '<textarea name="text" rows="1" cols="20"></textarea>' +
        '<h1> Email </h1>' +
        '<textarea name="text" rows="1" cols="20"></textarea>' +
        '<input type="submit" value="Submit text" />' +
        '</body>' +
        '</html>';
    response.writeHead(200, { "Content-Type": "text/html" });
    response.write(body);
    response.end();
}

在哪里响应来自一遍吗?发布数据?为什么我不能在这个回调定义一个变量,然后用它的回调之外吗?有没有一种方法能有几件事是连续的,则该程序异步的休息吗?

Where did response come from again? postData? Why can't I define a variable in this "callback" and then use it outside of the callback? Is there a way to have a few things be sequential then the rest of the program async?

推荐答案

我不知道正在使用此功能在哪里,但回调的一点是,要将其传递到运行异步一些功能;它存储回调走,当该功能正在与不管它需要做的,它会的呼叫的你提供必要的参数回调完成。一个例子由前到后的可能是最好的。

I'm not sure where this function is being used, but the point of callbacks is that you pass them into some function that runs asynchronously; it stores your callback away, and when that function is done with whatever it needs to do, it will call your callback with the necessary parameters. An example from front-to-back is probably best.

假设我们有一个框架,并在它存在着很长一段时间运行时,从数据库中提取某些数据的操作。

Imagine we have a framework, and in it there is an operation that runs for a long time, fetching some data from the database.

function getStuffFromDatabase() {
  // this takes a long time
};

由于我们不希望它同步运行,我们将允许用户在回调中传递。

Since we don't want it to run synchronously, we'll allow the user to pass in a callback.

function getStuffFromDatabase(callback) {
  // this takes a long time
};

我们会模拟花费很长的时间跟的setTimeout 通话;我们还将pretend我们从数据库中的一些数据,但我们会只是很难code的字符串值。

We'll simulate taking a long time with a call to setTimeout; we'll also pretend we got some data from the database, but we'll just hardcode a string value.

function getStuffFromDatabase(callback) {
  setTimeout(function() {
    var results = "database data";
  }, 5000);
};

最后,一旦我们拥有的数据,我们的呼叫的该框架的功能的用户给我们的回调。

Finally, once we have the data, we'll call the callback given to us by the user of the framework's function.

function getStuffFromDatabase(callback) {
  setTimeout(function() {
    var results = "database data";
    callback(results);
  }, 5000);
};

由于框架的用户,你会做这样的事情使用的功能:

As a user of the framework, you'd do something like this to use the function:

getStuffFromDatabase(function(data) {
  console.log("The database data is " + data);
});

所以,你可以看到数据(等同于响应 POSTDATA 在你的例子)来自您传递回调的到函数的;它给出了数据,你什么时候知道的数据应该是什么。

So, as you can see data (same as response and postData in your example) came from the function that you pass your callback into; it gives that data to you when it knows what that data should be.

您不能设置在回调的值,并用它回调以外的原因是因为回调本身不会发生,直到后来在时间。

The reason you can't set a value in your callback and use it outside the callback is because the callback itself doesn't happen until later in time.

//  executed immediately  executed sometime in the future
//      |                  |       by getStuffFromDatabase
//      v                  v
getStuffFromDatabase(function(data) {
  var results = data; // <- this isn't available until sometime in the future!
});

console.log(results); // <- executed immediately

在该的console.log 运行时,的 VAR结果赋值还没有发生!

When the console.log runs, the assignment of var results hasn't happened yet!

这篇关于了解异步code通俗地说的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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