简体   繁体   English

JavaScript,返回 promise 内箭头 function

[英]JavaScript, return promise inside arrow function

I've just seen this code:我刚刚看到了这段代码:

const buildSW = () => {
  // This will return a Promise <- that comment was present on the code
  workboxBuild
    .injectManifest({
      swSrc: 'src/sw.js'
      // some code
    })
    .then(({ count, size, warnings }) => {
      // some code
    })
    .catch(console.error);
};
buildSW();

The code seems to be working, has been in production like 1 year, but I get confused with the comment of This will return a Promise because I don't see that there is actually a return statement, and the whole code is inside { }.该代码似乎正在运行,已经投入生产大约 1 年,但我对This will return a Promise的评论感到困惑,因为我没有看到实际上有一个return语句,整个代码在 { } .

Shouldn't be?:不应该吗?:

return workboxBuild
    .injectManifest({ ...etc

The code is part from this guides:该代码是本指南的一部分:

https://developers.google.com/web/tools/workbox/guides/generate-service-worker/workbox-build https://developers.google.com/web/tools/workbox/guides/generate-service-worker/workbox-build

where the return promise is present there.那里存在return promise 。 So how or why is working without return in the code showed above?那么如何或为什么在上面显示的代码中没有返回?

The comment is commenting on the line following it, and is redundant.该注释是对其后行的注释,并且是多余的。 Eg例如

// a() will return a Promise
a().then(() => () // duh

...because it's equivalent to: ...因为它相当于:

const promise = a();
promise.then(() => ()

Shouldn't be?: return workboxBuild.injectManifest({...etc不应该吗?: return workboxBuild.injectManifest({...etc

It depends on the contract you want buildSW to have.这取决于您希望buildSW拥有的合同。

As written, buildSW returns nothing and handles its own errors, using .catch(console.error) to emit them to console.如所写, buildSW返回任何内容并处理自己的错误,使用.catch(console.error)将它们发送到控制台。 Appropriate for event handlers — Eg button.onclick = buildSW .适用于事件处理程序 - 例如button.onclick = buildSW

But if you expect to call buildSW from other places, a better contract is to return a promise, and leave error handling to the caller:但是如果你希望从其他地方调用buildSW ,一个更好的合约是返回一个 promise,并将错误处理留给调用者:

const buildSW = () => {
  return workboxBuild.injectManifest({swSrc: 'src/sw.js'})
    .then(({ count, size, warnings }) => {
      // some code that may also fail
    });
};
buildSW().catch(console.error);

I get confused with the comment because I don't see that there is actually a return statement, like it is present in the guide.我对评论感到困惑,因为我没有看到实际上有一个return声明,就像它出现在指南中一样。 Shouldn't it have one?不应该有一个吗?

Yes, there should be a return statement.是的,应该有return声明。 Sure, the function already catches errors and logs them, but it's still a good idea to always return a promise from asynchronous functions.当然,function 已经捕获并记录错误,但始终从异步函数返回 promise 仍然是个好主意。

So how or why is working without return in the code showed above?那么如何或为什么在上面显示的代码中没有返回?

It's working only because the buildSW();它的工作只是因为buildSW(); call you're doing doesn't try to use the promise that should be returned. call you're doing 不会尝试使用应该返回的 promise。

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

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