# Proxy: await trap?

**URL:** <https://es.discourse.group/t/proxy-await-trap/1688>\
**Category:** I have questions\
**Created:** [April 11, 2023, 10:42am UTC](https://es.discourse.group/t/proxy-await-trap/1688 "2023-04-11T10:42:55Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![nuxodin](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/nuxodin/32/1715_2.png) [@nuxodin](https://es.discourse.group/u/nuxodin)\
**Post date:** [April 11, 2023, 10:42am UTC](https://es.discourse.group/t/proxy-await-trap/1688/1 "2023-04-11T10:42:55Z")

</div>

Hello ECMAScript community,

I have been working with JavaScript Proxies and Promises, and I'm looking for a way to detect whether an object is being awaited using the `await` keyword or accessed with a chaining operator like `.`. Currently, JavaScript doesn't seem to have a built-in way to distinguish between these two cases. I was wondering if it would be possible to introduce a new "await trap" or "Promise trap" for Proxies in ECMAScript to handle this scenario.

The main idea is to have a trap that would be called when the object is awaited, allowing the Proxy to take different actions based on whether it's being used as a Promise or as a regular object with chaining.

Here is an example of what I'm trying to achieve:

```javascript
const handler = {
  get(target, prop, receiver) {
    // Handle regular properties and chaining
  },
  await(target) {
    // Handle await keyword and Promise resolution
  },
};

const p = new Proxy(target, handler);

// Using the object as a Promise
await p;

// Using the object with chaining
p.then;

```

I would appreciate any thoughts or feedback on this idea. Is there a reason this hasn't been implemented yet, or are there any alternative ways to achieve the same goal?

Thank you for your time and consideration.

---

<div class="post-metadata">

**Author:** ![ljharb](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/ljharb/32/8_2.png) [@ljharb](https://es.discourse.group/u/ljharb)\
**Post date:** [April 11, 2023, 4:23pm UTC](https://es.discourse.group/t/proxy-await-trap/1688/2 "2023-04-11T16:23:37Z")

</div>

`await` is just sugar for a nested Promise `.then`, and it's not an observable call by design - so no, there's no trap, and it's very intentionally not something you're allowed to interfere with.

Separately, I think it would be a very confusing world if, for a Promise `x`, `await x` and `return x` in an async function and `x.then()` (modulo someone monkeying with Promise.prototype.then) didn't do the same thing.

---

<div class="post-metadata">

**Author:** ![nuxodin](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/nuxodin/32/1715_2.png) [@nuxodin](https://es.discourse.group/u/nuxodin)\
**Post date:** [April 11, 2023, 7:14pm UTC](https://es.discourse.group/t/proxy-await-trap/1688/3 "2023-04-11T19:14:53Z")

</div>

Thank you for your response. I understand the design decision behind not having an "await trap" and the importance of consistency in the language.

In my use case, I am trying to build a library that allows asynchronous queries on data structures. The goal is to provide a chained syntax that simplifies the query writing. Using Proxies, I can achieve this as follows:

```javascript
await root.item('folder').item('subfolder').item('then').value 
// same but using proxies:
await proxify(root).folder.subfolder.then; // "then" is the name of the file

```

However, due to the behavior of `await`, the only identifier I cannot use in this context is `then`.

Introducing a `Symbol.then` as an additional alternative to `.then` for thanables would also help.

Maybe I just have to think about it more...

I appreciate your insight and will continue exploring different approaches to address this challenge.

---

<div class="post-metadata">

**Author:** ![ljharb](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/ljharb/32/8_2.png) [@ljharb](https://es.discourse.group/u/ljharb)\
**Post date:** [April 11, 2023, 7:25pm UTC](https://es.discourse.group/t/proxy-await-trap/1688/4 "2023-04-11T19:25:45Z")

</div>

You may be interested in [https://github.com/tc39/proposal-eventual-send](https://github.com/tc39/proposal-eventual-send)

---

<div class="post-metadata">

**Author:** ![senocular](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/senocular/32/642_2.png) [@senocular](https://es.discourse.group/u/senocular)\
**Post date:** [April 11, 2023, 9:44pm UTC](https://es.discourse.group/t/proxy-await-trap/1688/5 "2023-04-11T21:44:27Z")

</div>

It sounds like you need [Symbol.thenable](https://github.com/devsnek/proposal-symbol-thenable).

---

<div class="post-metadata">

**Author:** ![nuxodin](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/nuxodin/32/1715_2.png) [@nuxodin](https://es.discourse.group/u/nuxodin)\
**Post date:** [April 11, 2023, 9:57pm UTC](https://es.discourse.group/t/proxy-await-trap/1688/6 "2023-04-11T21:57:08Z")

</div>

Exactly, that would solve my problem.  
Proposal-eventual-send would probably solve the problem too, but it's a little harder to understand.  
Is that why the proposal was archived?

---

<div class="post-metadata">

**Author:** ![nuxodin](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/nuxodin/32/1715_2.png) [@nuxodin](https://es.discourse.group/u/nuxodin)\
**Post date:** [April 11, 2023, 10:01pm UTC](https://es.discourse.group/t/proxy-await-trap/1688/7 "2023-04-11T22:01:47Z")

</div>

Just for the record in this thread:  
For the same purpose (To not occupy property-names with foreign functionality) I support the idea of a `Symbol.toJSON`.

> [@New well-known Symbol: Symbol.toJSON](https://es.discourse.group/t/new-well-known-symbol-symbol-tojson/322):
>
> Hey ECMA community, I'm working on a library Boa that creates Proxy objects for binding to Python's object, but it fails on calling JSON.stringify with these proxy objects because it's circular as [https://github.com/alibaba/pipcook/issues/146](https://github.com/alibaba/pipcook/issues/146). And I just found that JSON.stringify will call the obj.toJSON() internally, so i'm wondering if a new well-known Symbol(.toJSON) instead of the current string keyed, just like what we have achieved on toPrimitive and toStringTag? If that makes sense, I'…

---

<div class="post-metadata">

**Author:** ![mhofman](https://avatars.discourse-cdn.com/v4/letter/m/f14d63/32.png) [@mhofman](https://es.discourse.group/u/mhofman)\
**Post date:** [April 12, 2023, 3:57am UTC](https://es.discourse.group/t/proxy-await-trap/1688/8 "2023-04-12T03:57:06Z")

</div>

The eventual-send proposal is very much not archived. It hasn't see much activity lately, but our production system actually uses a shim for it. It is meant to pair with the wavy-dot proposal to implement promise pipelining (our in our current implementation, a single letter `E` helper). There are a few posts in this forum discussing solution to this category of problems, some of which where I've posted more details on these 2 proposals.

---

<div class="post-metadata">

**Author:** ![nuxodin](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/nuxodin/32/1715_2.png) [@nuxodin](https://es.discourse.group/u/nuxodin)\
**Post date:** [April 12, 2023, 5:12am UTC](https://es.discourse.group/t/proxy-await-trap/1688/9 "2023-04-12T05:12:33Z")

</div>

No, I expressed myself badly, "Symbol.thenable" is archived.  
Good to know that a solution is developing for my problem.  
Thank you for your work.
