英文:
Why resolve parameter comes first in JavaScript Promises?
问题
我正在学习JavaScript的Promises。对我来说,它看起来像JavaScript的Promise
与callback
函数类似。
在callback
函数中,通常我们将第一个参数用作error
,而第二个参数用作success
。
那么为什么在JavaScript的Promise
中,参数的顺序不同呢?第一个是success
,第二个是error
?这就像是callback
参数结构的相反,这让我感到困惑。
这不是一个问题,但我想要一个解释。如果我错了,那么我错在哪里?
英文:
I was learning javascript Promises. To me, it looked like JavaScript Promise
works similar to a callback
function.
In a callback
function, we usually use the first parameter as an error
, and the second as a success
.
Then why in JavaScript Promise
, parameters come differently? First is the success
and second is the error
? It's like the opposite of the callback
parameter structure and it confuses me.
This isn't a problem, but I would like to have an explanation for this. If I am wrong, then what am I missing?
答案1
得分: 1
您显然在谈论Promise执行函数(传递给new Promise(...)
的内容)。这只是一种完全不同类型的回调,与标准的Node.js异步回调没有任何共同之处。
Promise执行函数会传递给您两个单独的函数,您以后可以调用它们。都不是错误。当您实际上遇到错误时,您调用reject(err)
,并将错误作为第一个参数传递给它。
另一个传递两个参数的地方是.then()
处理程序,您可以在其中传递解析处理程序和拒绝处理程序。同样,这是完全不同的东西。您正在传递两个函数引用,承诺基础结构将决定传递哪个回调。当调用这些回调时,将参数作为第一个参数传递。
在承诺中有两种不同的回调元素:
// 承诺执行函数
let p = new Promise((resolve, reject) => {
// 这是将两个函数引用传递给您的回调。
// 此时没有错误。
// 您可以在异步操作中决定调用哪个函数。
});
// 然后处理程序
somePromise.then(resolveData => {
// 当您的承诺解决时,将调用此回调
// 现在已知这里没有错误,因此无需传递err参数
}, rejectErr => {
// 当您的承诺被拒绝时,将调用此回调
// 错误将作为第一个参数传递
// 此回调是可选的
});
还有.catch()
和.finally()
处理程序,但它们以类似的方式工作,都接受一个回调函数。
请记住,模式:
p.then(successHandler, errorHandler)
不会将错误作为第二个参数传递给您。您正在传递两个函数引用,它将在以后的某个时间调用其中一个。从这个角度来看,它与fs.readFile()
等标准的Node.js异步回调不同。它有完全不同的目的,并且工作方式不同。
英文:
You're apparently talking about the Promise executor function (what you pass to new Promise(...)
). That's just an entirely different type of callback and it really doesn't have anything in common with the standard node.js asynchronous callback.
The promise executor function is passing you two separate functions that you can later call. Neither is an error. When you actually have an error, you call reject(err)
and you pass it the error as the first argument.
The other place that two things are passed is a .then()
handler where you can pass both a resolve handler and a reject handler. Again, this is a completely different thing. You are passing it two function references and the promise infrastructure will decide which callback to pass. When it calls those callbacks, it will pass the argument as the first argument.
Here are two different callback elements in promises:
// promise executor function
let p = new Promise((resolve, reject) => {
// this is passing to your callback two function references.
// there's no error at this point.
// you decide which function to call in your asynchronous operation
});
// then handler
somePromise.then(resolveData => {
// this callback gets called when your promise resolves
// it is known there is no error here, so no need to pass an err parameter
}, rejectErr => {
// this callback gets called when your promise is rejected
// the error is passed as the first argument
// this callback is optional
});
There are also .catch()
and .finally()
handlers, but they work similarly taking a single callback.
Keep in mind that the pattern:
p.then(successHandler, errorHandler)
is not passing you an error as the second argument. YOU are passing it two function references and it will call one of them as some later time. In this way, it is not like the standard nodejs asynchronous callback like fs.readFile()
would use. It has a completely different purpose and works differently.
答案2
得分: 0
你正在混淆JavaScript的Promise回调和Node标准库API中的回调设计模式。它们并不相同。
JavaScript Promise的目的是处理长时间运行的过程,在这些过程中,我们需要某种能力来确定何时完成该过程,以继续运行下一段代码。
因此,在你所学习的Promise回调中,有三种状态:未解决,已解决和已拒绝。
默认情况下,Promise存在于未解决状态,这意味着你刚刚创建了Promise,正在等待某些事情发生,例如Ajax请求或其他事件等长时间运行的过程。一旦发生,该Promise就会进入另外两种状态之一,即已解决或已拒绝。
上述内容并非Node标准库回调的目的和功能,这些回调用于文件系统API的函数中。这些特定函数,如read()
,readdir()
等,可以传递三个参数,其中一个是可选的,第三个就是你所说的回调。
在使用文件系统函数时,Node标准库回调的模式是在回调参数中提供两个参数,第一个始终是一个错误对象err
,现在不能保证会发生错误,如果在打开某些文件时出现问题,Node会使用err
对象的第一个参数来调用回调。
如果没有错误,那么第一个参数将为null。
现在,回调函数的第二个参数将是我实际正在寻找的数据。
https://nodejs.org/api/fs.html#fs_file_system
你可以看到,你在谈论的是两种不同的回调。
英文:
You are confusing JavaScript promises callbacks with callback design patterns from the Node standard library's API. They are not the same.
The purpose of JavaScript promises is to deal with long running processes where we need some ability to figure out when the process is finished to continue running the next snippet of code.
So in the Promises callbacks that you are learning about there are 3 states: unresolved, resolved, and rejected.
By default, a Promise exists in an unresolved state, that means you just made the Promise and you are waiting for something to happen, some long running process such as an Ajax request or some other event to occur, once it occurs, that Promises enters into either one of the two other states, resolved or rejected.
The above is not the purpose and function of Node standard library's callbacks which are used inside of functions that are part of the filesystem API. These particular functions such as read()
, readdir()
and so on have three arguments that can be passed to it, one being optional, the third being the callback which you speak of.
The pattern for Node standard library callbacks when working with filesystem functions is to offer two arguments in that callback argument, the first one always being an error object, err
, now it's not guaranteed that an error is going to occur, instead if something goes wrong when opening up some file(s), Node is going to call the callback with the first argument of the err
object.
If there is no error, then the first argument is going to instead be null.
Now the second argument to the callback function is going to be the data I am actually looking for.
https://nodejs.org/api/fs.html#fs_file_system
You see, two different callbacks you are talking about.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论