<tfoot id='8RN1H'></tfoot>
  • <legend id='8RN1H'><style id='8RN1H'><dir id='8RN1H'><q id='8RN1H'></q></dir></style></legend>

        <bdo id='8RN1H'></bdo><ul id='8RN1H'></ul>
    1. <small id='8RN1H'></small><noframes id='8RN1H'>

    2. <i id='8RN1H'><tr id='8RN1H'><dt id='8RN1H'><q id='8RN1H'><span id='8RN1H'><b id='8RN1H'><form id='8RN1H'><ins id='8RN1H'></ins><ul id='8RN1H'></ul><sub id='8RN1H'></sub></form><legend id='8RN1H'></legend><bdo id='8RN1H'><pre id='8RN1H'><center id='8RN1H'></center></pre></bdo></b><th id='8RN1H'></th></span></q></dt></tr></i><div id='8RN1H'><tfoot id='8RN1H'></tfoot><dl id='8RN1H'><fieldset id='8RN1H'></fieldset></dl></div>

        返回错误和抛出错误的区别

        Difference between return Error and throw Error(返回错误和抛出错误的区别)
          <tbody id='1tOfT'></tbody>
      1. <tfoot id='1tOfT'></tfoot>

        • <bdo id='1tOfT'></bdo><ul id='1tOfT'></ul>

          <legend id='1tOfT'><style id='1tOfT'><dir id='1tOfT'><q id='1tOfT'></q></dir></style></legend>

              <small id='1tOfT'></small><noframes id='1tOfT'>

                <i id='1tOfT'><tr id='1tOfT'><dt id='1tOfT'><q id='1tOfT'><span id='1tOfT'><b id='1tOfT'><form id='1tOfT'><ins id='1tOfT'></ins><ul id='1tOfT'></ul><sub id='1tOfT'></sub></form><legend id='1tOfT'></legend><bdo id='1tOfT'><pre id='1tOfT'><center id='1tOfT'></center></pre></bdo></b><th id='1tOfT'></th></span></q></dt></tr></i><div id='1tOfT'><tfoot id='1tOfT'></tfoot><dl id='1tOfT'><fieldset id='1tOfT'></fieldset></dl></div>

                1. 本文介绍了返回错误和抛出错误的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  在一个项目中发现以下代码,看不懂:

                  I found the following code in a project, that I do not understand.:

                  get(key, store = null) {
                      if (!key) {
                        return new Error('There is no key to get!');
                      }
                  
                      let dbstore = this.localforage;
                  
                      if (store !== null) {
                        dbstore = store;
                      }
                  
                      return dbstore
                        .getItem(key)
                        .then(function(value) {
                          return value;
                        })
                        .catch(function(err) {
                          return new Error('The key (' + key + ") isn't accessible: " + err);
                        });
                    }
                  

                  为什么return new Error('There is no key to get!'); 而不是throw new Error('There is no key to get!');?

                  为什么不在 catch 块中抛出错误?

                  Also why not throw an error in the catch block?

                  推荐答案

                  当你设计一个函数接口并且有错误需要处理时,你就有了如何返回错误的设计选择.如果函数是同步的,您可以返回一些指示错误并且很容易与实际结果区分开来的标记值(在 Javascript 中通常为 null),或者您可以 throw异常,或者您可以返回具有指示操作成功或失败的属性的对象.

                  When you design a function interface and there are errors to deal with, you have a design choice for how to return errors. If the function is synchronous, you can either return some sentinel value that indicates an error and is easily distinguished from an actual result (often null in Javascript) or you can throw an exception or you can return an object that has a property that indicates the success or failure of the operation.

                  当您使用 Promise 接口进行异步操作时,通常会使用 Error 对象拒绝 Promise 作为表示错误的拒绝原因.这就是 Promise 的核心设计理论.成功以可选值解决,错误以原因拒绝.

                  When you have an asynchronous operation with a promise interface, one would usually reject the Promise with an Error object as the reject reason to signify an error. That's the core design theory of promises. Success resolves with an optional value, errors reject with a reason.

                  这段代码:

                  return dbstore
                    .getItem(key)
                    .then(function(value) {
                      return value;
                    })
                    .catch(function(err) {
                      return new Error('The key (' + key + ") isn't accessible: " + err);
                    });
                  

                  正在使用值或 Error 对象解析返回的承诺.这通常不是 promise 代码的编写方式,因为它需要调用者测试解析值的类型以确定是否存在错误,这不是使用 promise 的简单、直接的方法.所以,对于你的问题,你通常会这样做:

                  is resolving the returned promise with either a value or an Error object. This is generally not how promise code is written because it will require the caller to test the type of the resolved value to figure out if there's an error or not which is not the simple, straightforward way to use promises. So, to your question, you would usually do this:

                  return dbstore.getItem(key).catch(function(err) {
                      throw new Error('The key (' + key + ") isn't accessible: " + err);
                  });
                  

                  这个函数还有其他迹象,它只是糟糕的代码.

                  There are other signs in this function, that it's just bad code.

                  1. .then(function(value) {return value;}) 完全是多余和不必要的.它根本没有增加任何价值.value 已经是 promise 的解析值.无需再次声明.

                  1. .then(function(value) {return value;}) is completely superfluous and unnecessary. It adds no value at all. The value is already the resolved value of the promise. No need to declare it again.

                  该函数有时会返回一个承诺,有时会抛出一个同步异常.
                  这甚至是一个进一步的使用痛苦.如果您查看第一个 if (!key) { 语句,它会返回一个错误对象,即未提供 key 参数.这意味着要使用此函数,您必须捕获同步异常,提供 .then().catch() 处理程序并检查已解析承诺的类型以查看是否它恰好是一个错误对象.使用此功能是一场噩梦.这是糟糕的代码.

                  The function sometimes returns a promise and sometimes throws a synchronous exception.
                  This is even a further pain to use. If you look at the first if (!key) { statement, it returns an Error object is the key argument isn't supplied. That means that to use this function you have to catch synchronous exceptions, provide .then() and .catch() handlers AND check the type of the resolved promise to see if it happens to be an error object. This function is a nightmare to use. It's bad code.

                  要按原样使用函数,调用者可能必须这样做:

                  To use the function as it is, the caller would likely have to do this:

                  let retVal = someObj.get(aKey);
                  if (typeof retVal === Error) {
                      // got some synchronous error
                  } else {
                      retVal.then(val => {
                          if (typeof val === Error) {
                              // got some asynchronous error
                          } else {
                              // got an actual successful value here
                          }
                      }).catch(err => {
                          // got some asynchronous error
                      })
                  }
                  

                  函数实现大概应该是这样的:

                  The function implementation probably should be this:

                  get(key, store = null) {
                      if (!key) {
                          return Promise.reject(new Error('There is no key to get!'));
                      }
                  
                      let dbstore = store || this.localforage;
                  
                      return dbstore.getItem(key).catch(function(err) {
                          throw new Error('The key (' + key + ") isn't accessible: " + err);
                      });
                  }
                  

                  然后可以这样使用:

                  someObj.get(aKey).then(val => {
                      // got some successful value here
                  }).catch(err => {
                      // got some error here
                  });
                  

                  将这里调用者的简单性与上面的混乱进行比较.

                  Compare the simplicity for the caller here to the mess above.

                  此实现具有以下一致性:

                  This implementation has these consistencies:

                  1. 它总是返回一个承诺.如果没有提供 key,它会返回一个被拒绝的 promise.
                  2. 所有错误都来自被拒绝的承诺
                  3. promise 解析的值始终是实际成功的值
                  4. 没有没有任何用处的 .then() 处理程序.
                  1. It always returns a promise. If key isn't supplied, it returns a rejected promise.
                  2. All errors come via a rejected promise
                  3. The value the promise resolves with is always an actual successful value
                  4. There's no .then() handler that does nothing useful.

                  这篇关于返回错误和抛出错误的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Pause youtube video, youtube api(暂停 youtube 视频,youtube api)
                  Youtube iframe api not triggering onYouTubeIframeAPIReady(Youtube iframe api 未触发 onYouTubeIframeAPIReady)
                  How can I stop a video with Javascript in Youtube?(如何在 Youtube 中停止使用 Javascript 的视频?)
                  How to call Greasemonkey#39;s GM_ functions from code that must run in the target page scope?(如何从必须在目标页面范围内运行的代码中调用 Greasemonkey 的 GM_ 函数?)
                  How do you mute an embedded Youtube player?(如何使嵌入式 Youtube 播放器静音?)
                  How to get number of video views with YouTube API?(如何使用 YouTube API 获取视频观看次数?)
                    • <tfoot id='PovLS'></tfoot>
                        <bdo id='PovLS'></bdo><ul id='PovLS'></ul>
                        <i id='PovLS'><tr id='PovLS'><dt id='PovLS'><q id='PovLS'><span id='PovLS'><b id='PovLS'><form id='PovLS'><ins id='PovLS'></ins><ul id='PovLS'></ul><sub id='PovLS'></sub></form><legend id='PovLS'></legend><bdo id='PovLS'><pre id='PovLS'><center id='PovLS'></center></pre></bdo></b><th id='PovLS'></th></span></q></dt></tr></i><div id='PovLS'><tfoot id='PovLS'></tfoot><dl id='PovLS'><fieldset id='PovLS'></fieldset></dl></div>

                        <small id='PovLS'></small><noframes id='PovLS'>

                          <tbody id='PovLS'></tbody>
                        <legend id='PovLS'><style id='PovLS'><dir id='PovLS'><q id='PovLS'></q></dir></style></legend>