简体   繁体   English

这些参数是否“扩展”在 Promise 中解析和拒绝?

[英]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? dataerr在这里代表解决和拒绝吗? 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;
        }
 );
  1. It's passing two fat arrow functions to .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.第一个是 function 如果 promise 得到解决,则调用,第二个是 function 如果被拒绝,则调用。 Logically, the structure of this is .then(resolveCalback, rejectCallback) .从逻辑上讲,它的结构是.then(resolveCalback, rejectCallback)
  2. The first fat arrow function has two arguments, (data, err) .第一个粗箭头 function 有两个 arguments, (data, err) That's wrong.那是错误的。 With any standard promise implementation, there is only one argument ever passed to a .then() handler.对于任何标准的 promise 实现,只有一个参数传递给.then()处理程序。 Fortunately, the implementation does not use the second argument which would always be undefined anyway.幸运的是,该实现不使用第二个参数,该参数无论如何总是undefined的。
  3. That first fat arrow function just moves the resolved value from data to ctx.body .第一个粗箭头 function 只是将解析值从data移动到ctx.body Apparently, the caller of the function that returns this promise will look for the resolved value in ctx.body .显然,返回此 promise 的 function 的调用者将在ctx.body中查找已解析的值。
  4. The second fat arrow function is also setting up the ctx object with a status and error value.第二个粗箭头 function 也在设置带有状态和错误值的ctx object。 And, importantly, it is NOT rethrowing or returning a rejected promise.而且,重要的是,它不会重新抛出或返回被拒绝的 promise。 So, it's changing the promise from rejected to resolved.因此,它将 promise 从拒绝更改为已解决。 So, the caller will only ever get a resolved promise from this function and will apparently have to examine ctx.response.status to know if there is an error or not.因此,调用者只会从这个 function 中得到一个已解析的 promise 并且显然必须检查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 .您可以在 MDNSitePoint上阅读有关胖箭头功能的信息。 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.

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