在后台如何用Javascript编码回调? [英] How are callbacks coded in Javascript, behind the scenes?

查看:52
本文介绍了在后台如何用Javascript编码回调?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

函数如何知道某事是回调;某些先前的I/O完成后需要执行的操作.它怎么知道它不应该立即执行?是在函数中以(标准化)方式定义的吗?

How does a function know that something is a callback; something that needs to be executed once certain prior I/O has completed. How does it know that it shouldn't execute right away? Is it defined in a function in a (standardized) way?

据我所知,在参数中经常使用"callback"关键字只是一种常见的做法,但不会自动让函数将参数解释为某些I/O完成后应该开始的参数.

As far as I know the 'callback' keyword that is often used in an argument is just common practise, but does not automatically let the function interpret the argument as something that should start once certain I/O has completed.

以下面的示例为例,我有两个问题(摘自

Taking the below example, I have two questions (taken from https://medium.com/codebuddies/getting-to-know-asynchronous-javascript-callbacks-promises-and-async-await-17e0673281ee):

const request = require(‘request’);
function handleResponse(error, response, body){
    if(error){
        // Handle error.
    }
    else {
        // Successful, do something with the result.
    }
}
request('https://www.somepage.com', handleResponse);

  1. 'require'函数的结构是什么样子,以便它知道一旦请求完成,就应该执行参数2(在这种情况下为handleResponse)?我想这可以归结为我上面问过的相同问题.

  1. What does the structure of the 'require' function look like so that it knows that argument 2 (handleResponse in this case) should be executed once the request has completed? I guess this gets down to the same question that I asked above.

即使函数中没有async关键字,函数也可以异步进行吗?如果是,浏览器如何知道它是异步函数?

Can functions be asynchronous even without the async keyword in the function? If yes, how does the browser know it's an asynchronous function?

推荐答案

  1. 我假设您要询问有关 request 函数的信息.大多数异步函数会调用其他异步函数来执行某些工作,然后调用回调.您可以将其视为函数和回调链.这就是 request 函数的作用.当然,至少最后一个函数需要真正地异步,并且通常是由节点内置模块之一( fs http ,)导出的函数...).它也可以由本机模块导出,或者在最新的节点版本中使用 worker_threads 模块导出.

  1. I assume you meant to ask about the request function. Most asynchronous functions call other asynchronous functions to do some work, and then call the callback. You can think of it as a chain of functions and callbacks. This is what the request function does. Of course, at least the last function needs to be truly asynchronous, and usually that is a function exported by one of the node built-in modules (fs, http, ...). It may also be exported by a native module, or using the worker_threads module in more recent node versions.

对于异步功能,不需要 async 关键字.将功能标记为 async 可使该功能使用 await 语法,并使该功能隐式返回Promise.

The async keyword isn't required for a function to be asynchronous. Marking a function as async lets the function use the await syntax and makes the function return a Promise implicitly.

为了更好地理解异步代码的工作原理,您应该看看节点事件循环有效

To better understand how asynchronous code works, you should take a look at how the node event loop works

请注意,由于您使用了 require ,因此我假设使用Node.js环境,但是我所说的大部分内容也适用于浏览器:有一串异步函数,最后一个是异步函数.将调用内置函数之一(例如 XMLHttpRequest ).浏览器环境也使用事件循环,并且 async/await 以相同的方式工作

Note that I assumed a Node.js environment, because you used require, but most of what I said applies to the browser as well: there is a chain of asynchronous functions, and the last one will call one of the built-in functions (for instance XMLHttpRequest). The browser environment uses an event loop too, and async/await work in the same way

这篇关于在后台如何用Javascript编码回调?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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