Skip to content
This repository was archived by the owner on Feb 26, 2021. It is now read-only.
This repository was archived by the owner on Feb 26, 2021. It is now read-only.

Add then() hook which returns null #38

@dko-slapdash

Description

@dko-slapdash

Hi.

This line:

get: (target, key) => {

The essence is that oc() object cannot be currently returned as a Promise (see StackOverflow link below).

So to work-around, I had to wrap the oc() proxy into one more proxy which says that there is no then() hook defined explicitly:

  async json<TRes>() {
    const json = await (await this.fetch()).json();
    const ocProxy = oc(json as TRes);
    // This is crazy, but no-one can return Promise<Proxy> and live...
    // Because await calls Proxy's then() hook which doesn't exist in oc().
    // https://stackoverflow.com/a/53890904
    return new Proxy(ocProxy, {
      get: (target, key) => (key == "then" ? null : (target as any)[key])
    }) as typeof ocProxy;
  }
...
const res = await obj.json<{xyz: string}>();
console.log(res.xyz()); // hangs if json() doesn't do the trick and just returns the oc()

There could be a simple fix to oc() by just checking key for "then" - the same way as in the example above.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions