attempt | WebHare Platform SDK
WebHare Platform SDK
    Preparing search index...

    Function attempt

    • Attempt to run code that may throw. If it throws anything, the fallback is returned

      This allows you to replace:

      let url;
      try {
      url = new URL(inurl);
      } catch (e) {
      return null;
      }```

      with

      const url = attempt(() => new URL(inurl));

      
      

      Type Parameters

      • T

      Parameters

      • toTry: Promise<T> | (() => Promise<T>)

      Returns Promise<T | undefined>

    • Attempt to run code that may throw. If it throws anything, the fallback is returned

      This allows you to replace:

      let url;
      try {
      url = new URL(inurl);
      } catch (e) {
      return null;
      }```

      with

      const url = attempt(() => new URL(inurl));

      
      

      Type Parameters

      • T
      • F

      Parameters

      • toTry: Promise<T> | (() => Promise<T>)
      • fallback: F

      Returns Promise<T | F>

    • Attempt to run code that may throw. If it throws anything, the fallback is returned

      This allows you to replace:

      let url;
      try {
      url = new URL(inurl);
      } catch (e) {
      return null;
      }```

      with

      const url = attempt(() => new URL(inurl));

      
      

      Type Parameters

      • T

      Parameters

      • toTry: () => T

      Returns T | undefined

    • Attempt to run code that may throw. If it throws anything, the fallback is returned

      This allows you to replace:

      let url;
      try {
      url = new URL(inurl);
      } catch (e) {
      return null;
      }```

      with

      const url = attempt(() => new URL(inurl));

      
      

      Type Parameters

      • T
      • F

      Parameters

      • toTry: () => T
      • fallback: F

      Returns T | F