用外行的术语理解异步代码 [英] Understanding Asynchronous Code in Layman's terms

查看:26
本文介绍了用外行的术语理解异步代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我理解异步性的基本原理:事物不是按顺序执行的.而且我知道有一些非常强大的东西......据称.但是对于我的生活,我无法将头围绕在代码上.让我们来看看我写过的异步 Node.JS 代码......但并没有真正理解.

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 来模拟花费很长时间;我们还会假装从数据库中获取了一些数据,但我们只会对字符串值进行硬编码.

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);
});

因此,如您所见,data(与示例中的 responsepostData 相同)来自您传递回调的函数进入;当它知道该数据应该是什么时,它就会向您提供该数据.

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 results 的赋值还没有发生!

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

这篇关于用外行的术语理解异步代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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