# Throw in none async func

**URL:** <https://forum.dfinity.org/t/throw-in-none-async-func/8516>\
**Category:** Developers\
**Created:** [November 11, 2021, 11:30am UTC](https://forum.dfinity.org/t/throw-in-none-async-func/8516 "2021-11-11T11:30:32Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![peterparker](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@peterparker](https://forum.dfinity.org/u/peterparker)\
**Post date:** [November 11, 2021, 11:30am UTC](https://forum.dfinity.org/t/throw-in-none-async-func/8516/1 "2021-11-11T11:30:32Z")

</div>

Is there a way to use throw errors (`throw Error.reject`) in none async function?  
Or is there a way to throw an exception in a query function in a none async context?

* * *

In one of my canister I have got the following query function:

```auto
public shared query({ caller }) func getStorage() : async ?BucketId {
        let result: {#bucketId: ?BucketId; #error: Text;} = storagesStore.getBucket(caller);

        switch (result) {
            case (#error error) {
                throw Error.reject(error);
            };
            case (#bucketId bucketId) {
                switch (bucketId) {
                    case (?bucketId) {
                        return ?bucketId;
                    };
                    case null {
                        return null;
                    };
                };
            };
        };
    };

```

I’ve made my storage generic and therefore would like to refactor it to use it multiple times:

```auto
public shared query({ caller }) func getStorage(): async (?BucketId) {
        return getBucket<StorageBucket>(caller, storagesStore);
    };

public shared query({ caller }) func getWhateverBucket(): async (?BucketId) {
        return getBucket<AnotherTypeOfBucket>(caller, myOtherStore);
    };

private func getBucket<T>(caller: Principal, store: BucketsStore.BucketsStore<T>): (?BucketId) {
        let result: {#bucketId: ?BucketId; #error: Text;} = store.getBucket(caller);

        switch (result) {
            case (#error error) {
                throw Error.reject(error);
            };
            case (#bucketId bucketId) {
                switch (bucketId) {
                    case (?bucketId) {
                        return ?bucketId;
                    };
                    case null {
                        return null;
                    };
                };
            };
        };
    };

```

However, I cannot do so as `throw Error.reject` needs an async context (`misplaced throw`).

---

<div class="post-metadata">

**Author:** ![nomeata](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/nomeata/32/733_2.png) [@nomeata](https://forum.dfinity.org/u/nomeata)\
**Post date:** [November 11, 2021, 11:58am UTC](https://forum.dfinity.org/t/throw-in-none-async-func/8516/2 "2021-11-11T11:58:38Z")

</div>

No, unfortunately you cannot abstract over `throw`, just like you can’t directly abstract over `await`. The “official” solution is to have the helper function also have an `async` return type, then this works. But

- You can then only return sharable type (although it’s not actually serialized, but transferred in the heap)
- You have to call that function in `await`, which means that the commit/rollback behavior is changed
- Because of this is a self-inter-canister-call, this is not possible from query methods

For more background on this design, [Support direct abstraction of code that awaits into functions, without requiring an unnecessary async · Issue #1482 · dfinity/motoko · GitHub](https://github.com/dfinity/motoko/issues/1482) and [Locally abstracting over async calls · Issue #680 · dfinity/motoko · GitHub](https://github.com/dfinity/motoko/issues/680) are relevant.

---

<div class="post-metadata">

**Author:** ![peterparker](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@peterparker](https://forum.dfinity.org/u/peterparker)\
**Post date:** [November 11, 2021, 12:07pm UTC](https://forum.dfinity.org/t/throw-in-none-async-func/8516/3 "2021-11-11T12:07:05Z")

</div>

Thanks for the detailed answer.

These functions (from my code snippet) are called from my webapp and have to be performant, therefore I cannot transform the helper to be async.

Don’t know what the best practice but, I guess then if I want to spare code I’ll have to drop `throw Error` and probably transform the return type to a variant that contains either the textual Error or the effective Result.

---

<div class="post-metadata">

**Author:** ![jzxchiang](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/jzxchiang/32/2592_2.png) [@jzxchiang](https://forum.dfinity.org/u/jzxchiang)\
**Post date:** [November 11, 2021, 9:59pm UTC](https://forum.dfinity.org/t/throw-in-none-async-func/8516/4 "2021-11-11T21:59:55Z")

</div>

Yeah, I had to use a `Result` for something like this.

---

<div class="post-metadata">

**Author:** ![peterparker](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@peterparker](https://forum.dfinity.org/u/peterparker)\
**Post date:** [November 12, 2021, 7:00am UTC](https://forum.dfinity.org/t/throw-in-none-async-func/8516/5 "2021-11-12T07:00:26Z")

</div>

`Result` is a new addition to the language?

I tried following in motoko playground but get the error `unbound type Result` 🤷‍♂️

```auto
import Result "mo:base/Result";

actor Echo {

  public query func say(phrase : Text) : async (Result<Text, Text>) {
    return #ok "yolo" ;
  };

};

```

---

<div class="post-metadata">

**Author:** ![PaulLiu](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/paulliu/32/4701_2.png) [@PaulLiu](https://forum.dfinity.org/u/PaulLiu)\
**Post date:** [November 12, 2021, 7:21am UTC](https://forum.dfinity.org/t/throw-in-none-async-func/8516/6 "2021-11-12T07:21:18Z")

</div>

This `import Result ...` imports the module `Result`, but not the type defined in this module. So you’ll need to write `Result.Result<Text, Text>`.

---

<div class="post-metadata">

**Author:** ![peterparker](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@peterparker](https://forum.dfinity.org/u/peterparker)\
**Post date:** [November 12, 2021, 7:23am UTC](https://forum.dfinity.org/t/throw-in-none-async-func/8516/7 "2021-11-12T07:23:32Z")

</div>

Thanks! Yes, it goes better after an early morning coffee 😉
