简体   繁体   English

像 fs.writeFile() 回调参数这样的函数是如何工作的?

[英]How do functions like fs.writeFile() callback arguements work?

I'm learning JavaScript at the moment, specifically Node JS.我目前正在学习 JavaScript,特别是 Node JS。 And during a basic tutorial I notice that some functions have callbacks with predefined arguments.在基础教程中,我注意到一些函数具有带有预定义参数的回调。 For example例如

fs.writeFile(
path.join(__dirname, 'test', 'hello.txt'), 
"Hello World", 
err => {
if (err) throw err;
console.log('File Created...')})

I want to know where was "err" defined as a ErrNoException type.我想知道“err”在哪里定义为ErrNoException类型。

Thanks!谢谢!

I want to know where was "err" defined as a ErrNoException type.我想知道“err”在哪里定义为 ErrNoException 类型。

In this type of interface, you pass a callback function and fs.writeFile() determines what arguments it will pass that callback when it calls it.在这种类型的接口中,您传递一个回调函数, fs.writeFile()确定它在调用该回调时将传递哪些参数。 It is up to you to declare your callback function to have appropriately named arguments that match what the caller will be passing it.您可以将回调函数声明为具有与调用者将传递的内容相匹配的适当命名的参数。 So, YOU must declare the callback with an appropriately named argument.因此,您必须使用适当命名的参数声明回调。 Since Javascript is not a typed language, you don't specific what the type of that argument is.由于 Javascript 不是类型化语言,因此您不必具体说明该参数的类型。 The type of the argument is determined by the code that calls the callback.参数的类型由调用回调的代码决定。 In this case that is the fs.writeFile() implementation.在这种情况下,这是fs.writeFile()实现。 To use the callback appropriately, you have to read the documentation for fs.writeFile() so that you know how to use the argument to the callback.要正确使用回调,您必须阅读fs.writeFile()的文档,以便您知道如何使用回调的参数。

So, in this specific case, fs.writeFile() requires a callback function and that callback function will be passed one argument and that argument will either by null (if there was no error) or will be an Error object that explains what the error was).因此,在这种特定情况下, fs.writeFile()需要一个回调函数,该回调函数将传递一个参数,该参数要么为null (如果没有错误)要么是一个 Error 对象,用于解释错误曾是)。 You must learn this by either reading the code for fs.writeFile() or reading some documentation for it.您必须通过阅读fs.writeFile()的代码或阅读它的一些文档来了解这一点。

It's important to realize that when you define your callback function, all you are doing is providing a name for the arguments you expect the callback to be called with.重要的是要意识到,当您定义回调函数时,您所做的只是为您希望调用回调的参数提供一个名称。 You can then use that name in your code to reference those arguments.然后,您可以在代码中使用该名称来引用这些参数。 The actual data that is passed to that callback when it is called will be supplied by the fs.writeFile() function when it calls the callback.调用回调时传递给该回调的实际数据将由fs.writeFile()函数在调用回调时提供。

It's unclear exactly what you mean by "defined as a ErrNoException type".目前还不清楚“定义为 ErrNoException 类型”的确切含义。 Nodejs has an internal function (not available for external use) called errNoException() and you can see the source here . Nodejs 有一个名为errNoException()的内部函数(不可用于外部使用errNoException() ,您可以在此处查看源代码。 It uses that function internally for creating an Error object with some common properties such as an error code and a stack trace.它在内部使用该函数来创建具有一些常见属性(例如错误代码和堆栈跟踪)的 Error 对象。

You can write similar functions yourself.您可以自己编写类似的函数。 Callback is simply a function (F) which is passed to another function(A) as a parameter.回调只是一个函数(F),它作为参数传递给另一个函数(A)。 Function (A) executes some tasks and then executes Function F from parameters.函数(A)执行一些任务,然后从参数执行函数F。 For example:例如:

function getData(url, callback) {
    fetch(url)
        .then(res => callback(null, res))
        .catch(err => callback(err, null))
}

In the sample function, what happens is, after making a fetch request, we wait until a response or error is generated.在示例函数中,发生的情况是,在发出 fetch 请求后,我们等待直到生成响应或错误。 If a response is there, we simply pass that to callback param 2 and call first param to null and vice versa.如果有响应,我们只需将其传递给回调参数 2 并将第一个参数调用为 null,反之亦然。 Now, while calling the function,现在,在调用函数时,

getData('http://stackoverflow.com', (err, res) => {
     if(err)
         return console.log(err)
     console.log(res)
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM