[英]Are these parameters “extended” resolve and reject in Promise?
I am reusing some code found on Github and I came up with some code that I am not sure what it is.我正在重用在 Github 上找到的一些代码,我想出了一些我不确定它是什么的代码。 This is the whole code;
这是整个代码;
const checkifRightPw = (ctx, next) => {
return new Promise((resolve, reject) => {
users.getUserByEmail(ctx.params.user_email).then((users) => {
if (users.length > 0) {
resolve(users);
} else {
reject('email doesnt exist');
}
});
}).then(
(data, err) => (ctx.body = data),
(err) => {
ctx.response.status = 404;
ctx.body = err;
}
);
}
I am wondering about these lines of code:我想知道这些代码行:
(data, err) => (ctx.body = data),
(err) => {
ctx.response.status = 404;
ctx.body = err;
}
Does data and err stand for resolve and reject here? data和err在这里代表解决和拒绝吗? Normally in promises there is a function call after.then so I am a bit confused about this situation here.
通常在承诺中会有一个 function 调用之后。所以我对这里的这种情况有点困惑。
OK, let's look at this:好的,让我们看看这个:
.then(
(data, err) => (ctx.body = data),
(err) => {
ctx.response.status = 404;
ctx.body = err;
}
);
.then()
..then()
。 The first is a function to be called if the promise is resolved, the second is a function to be called if it's rejected..then(resolveCalback, rejectCallback)
..then(resolveCalback, rejectCallback)
。(data, err)
.(data, err)
。 That's wrong..then()
handler..then()
处理程序。 Fortunately, the implementation does not use the second argument which would always be undefined
anyway.undefined
的。data
to ctx.body
.data
移动到ctx.body
。 Apparently, the caller of the function that returns this promise will look for the resolved value in ctx.body
.ctx.body
中查找已解析的值。ctx
object with a status and error value.ctx
object。 And, importantly, it is NOT rethrowing or returning a rejected promise.ctx.response.status
to know if there is an error or not.ctx.response.status
以了解是否存在错误。 If the fat arrow functions are confusing you, then this snippet of code could be expressed like this using regular functions:如果粗箭头函数让您感到困惑,那么这段代码可以使用常规函数表示如下:
.then(function(data) {
ctx.body = data;
return data;
}, function(err) {
ctx.response.status = 404;
ctx.body = err;
});
Does data and err stand for resolve and reject here?
data 和 err 在这里代表解决和拒绝吗?
No. See my second point above.不。见我上面的第二点。 Only
data
is valid and it contains the resolved value of the promise.只有
data
是有效的,它包含 promise 的解析值。 Having err
as an argument to that first clalback is a programming error.将
err
作为第一个回拨的参数是一个编程错误。 It is not passed there.它没有通过那里。 The reject callback is the second callback function.
拒绝回调是第二个回调 function。
Normally in promises there is a function call after.then so I am a bit confused about this situation here.
通常在承诺中会有一个 function 调用之后。所以我对这里的这种情况有点困惑。
These are fat arrow functions (a different type of function declaration).这些是粗箭头函数(不同类型的 function 声明)。 So, there are two functions being passed to like
.then(fn1, fn2)
.因此,有两个函数被传递给 like
.then(fn1, fn2)
。 You can read about fat arrow functions here on MDN and here on SitePoint .您可以在 MDN和SitePoint上阅读有关胖箭头功能的信息。 Their main purpose is a shortcut syntax and a different treatment for the value of
this
inside the function.它们的主要目的是一种快捷语法和对 function 中
this
值的不同处理。 In this case, the programmer likely used them for the shorter syntax since there are no references to this
.在这种情况下,程序员可能将它们用于较短的语法,因为没有对
this
的引用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.