• <legend id='ixXjC'><style id='ixXjC'><dir id='ixXjC'><q id='ixXjC'></q></dir></style></legend>

  • <tfoot id='ixXjC'></tfoot>
  • <small id='ixXjC'></small><noframes id='ixXjC'>

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

        <bdo id='ixXjC'></bdo><ul id='ixXjC'></ul>

        承诺解决后打字稿返回布尔值

        时间:2023-09-30

      1. <i id='SwLVU'><tr id='SwLVU'><dt id='SwLVU'><q id='SwLVU'><span id='SwLVU'><b id='SwLVU'><form id='SwLVU'><ins id='SwLVU'></ins><ul id='SwLVU'></ul><sub id='SwLVU'></sub></form><legend id='SwLVU'></legend><bdo id='SwLVU'><pre id='SwLVU'><center id='SwLVU'></center></pre></bdo></b><th id='SwLVU'></th></span></q></dt></tr></i><div id='SwLVU'><tfoot id='SwLVU'></tfoot><dl id='SwLVU'><fieldset id='SwLVU'></fieldset></dl></div>
      2. <legend id='SwLVU'><style id='SwLVU'><dir id='SwLVU'><q id='SwLVU'></q></dir></style></legend>
            <tbody id='SwLVU'></tbody>
            <tfoot id='SwLVU'></tfoot>
              <bdo id='SwLVU'></bdo><ul id='SwLVU'></ul>

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

                  本文介绍了承诺解决后打字稿返回布尔值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我试图在承诺解决后返回一个布尔值,但打字稿给出错误提示

                  I'm trying to return a boolean after a promise resolves but typescript gives an error saying

                  get"访问器必须返回一个值.

                  我的代码看起来像.

                  get tokenValid(): boolean {
                      // Check if current time is past access token's expiration
                      this.storage.get('expires_at').then((expiresAt) => {
                        return Date.now() < expiresAt;
                      }).catch((err) => { return false });
                  }
                  

                  此代码适用于 Ionic 3 应用程序,存储为 Ionic Storage 实例.

                  This code is for Ionic 3 Application and the storage is Ionic Storage instance.

                  推荐答案

                  你可以返回一个 Promise ,它会解析成这样的布尔值:

                  You can return a Promise that resolves to a boolean like this:

                  get tokenValid(): Promise<boolean> {
                    // |
                    // |----- Note this additional return statement. 
                    // v
                    return this.storage.get('expires_at')
                      .then((expiresAt) => {
                        return Date.now() < expiresAt;
                      })
                      .catch((err) => {
                        return false;
                      });
                  }
                  

                  您问题中的代码只有两个返回语句:一个在 Promise 的 then 处理程序中,一个在其 catch 处理程序中.我们在 tokenValid() 访问器中添加了第三条 return 语句,因为访问器也需要返回一些东西.

                  The code in your question only has two return statements: one inside the Promise's then handler and one inside its catch handler. We added a third return statement inside the tokenValid() accessor, because the accessor needs to return something too.

                  这是一个工作示例 在 TypeScript 游乐场:

                  class StorageManager { 
                  
                    // stub out storage for the demo
                    private storage = {
                      get: (prop: string): Promise<any> => { 
                        return Promise.resolve(Date.now() + 86400000);
                      }
                    };
                  
                    get tokenValid(): Promise<boolean> {
                      return this.storage.get('expires_at')
                        .then((expiresAt) => {
                          return Date.now() < expiresAt;
                        })
                        .catch((err) => {
                          return false;
                        });
                    }
                  }
                  
                  const manager = new StorageManager();
                  manager.tokenValid.then((result) => { 
                    window.alert(result); // true
                  });
                  

                  这篇关于承诺解决后打字稿返回布尔值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Ionic v3:按日期/天分组列表 下一篇:IONIC - 如何使用 JavaScript 从应用程序在 Play 商店中打开应用程序的反馈页面

                  相关文章

                      <tfoot id='HJ8Dc'></tfoot>

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

                      <legend id='HJ8Dc'><style id='HJ8Dc'><dir id='HJ8Dc'><q id='HJ8Dc'></q></dir></style></legend>
                        <bdo id='HJ8Dc'></bdo><ul id='HJ8Dc'></ul>

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