Problem: Proxy a Remote Object Over Network Simulation

Hard
40 min
Simulate remote property access where all reads and writes occur through async network calls.

Problem statement

Your team is building a client-side SDK that communicates with a remote configuration service. When developers access a property like remoteConfig.theme, it should transparently make a network request to fetch that value. Similarly, when they update a property (e.g., remoteConfig.theme = 'dark'), it should send a simulated update request.

You’ll build a Proxy that represents this remote object. It won’t hold any real data locally—instead, every read and write will trigger asynchronous “network” behavior under the hood. This will demonstrate how the Proxy Pattern can simulate remote procedure calls or client-side stubs that behave like local objects but operate remotely.

Goal

Implement createRemoteObject() that returns a Proxy where:

  • Reading any property fetches it asynchronously after a delay.

  • Writing to any property sends an async update (simulated via a console log).

Constraints

  • You must use both the get and set traps.

  • Simulate network latency using setTimeout and Promise.

  • The Proxy should not store state locally—each access or write should trigger a new simulated network call.

  • Reading a property should return a Promise that resolves to the value.

You don’t need to use Reflect.get() or Reflect.set() in this problem. Normally, Reflect is used to forward reads and writes to the Proxy’s target object, but here the Proxy replaces that behavior entirely.

Sample output

The examples below illustrate what the output should look like:

remoteConfig.theme.then(value => console.log('Fetched:', value));
remoteConfig.language.then(value => console.log('Fetched:', value));
// Writing properties
remoteConfig.theme = 'dark';
remoteConfig.language = 'fr';
/* Expected output (order may vary due to async timing):
Fetching theme...
Fetching language...
Updating theme to: dark
Updating language to: fr
Fetched: light
Fetched: en
*/

Good luck trying the problem! If you’re unsure how to proceed, check the “Solution” tab above.

Problem: Proxy a Remote Object Over Network Simulation

Hard
40 min
Simulate remote property access where all reads and writes occur through async network calls.

Problem statement

Your team is building a client-side SDK that communicates with a remote configuration service. When developers access a property like remoteConfig.theme, it should transparently make a network request to fetch that value. Similarly, when they update a property (e.g., remoteConfig.theme = 'dark'), it should send a simulated update request.

You’ll build a Proxy that represents this remote object. It won’t hold any real data locally—instead, every read and write will trigger asynchronous “network” behavior under the hood. This will demonstrate how the Proxy Pattern can simulate remote procedure calls or client-side stubs that behave like local objects but operate remotely.

Goal

Implement createRemoteObject() that returns a Proxy where:

  • Reading any property fetches it asynchronously after a delay.

  • Writing to any property sends an async update (simulated via a console log).

Constraints

  • You must use both the get and set traps.

  • Simulate network latency using setTimeout and Promise.

  • The Proxy should not store state locally—each access or write should trigger a new simulated network call.

  • Reading a property should return a Promise that resolves to the value.

You don’t need to use Reflect.get() or Reflect.set() in this problem. Normally, Reflect is used to forward reads and writes to the Proxy’s target object, but here the Proxy replaces that behavior entirely.

Sample output

The examples below illustrate what the output should look like:

remoteConfig.theme.then(value => console.log('Fetched:', value));
remoteConfig.language.then(value => console.log('Fetched:', value));
// Writing properties
remoteConfig.theme = 'dark';
remoteConfig.language = 'fr';
/* Expected output (order may vary due to async timing):
Fetching theme...
Fetching language...
Updating theme to: dark
Updating language to: fr
Fetched: light
Fetched: en
*/

Good luck trying the problem! If you’re unsure how to proceed, check the “Solution” tab above.

Node.js
// Simulated remote store
const remoteData = {
theme: 'light',
language: 'en'
};
// Your code here
// Example usage
const remoteConfig = createRemoteObject();
// Reading properties
remoteConfig.theme.then(value => console.log('Fetched:', value));
remoteConfig.language.then(value => console.log('Fetched:', value));
// Writing properties
remoteConfig.theme = 'dark';
remoteConfig.language = 'fr';
/* Expected output (order may vary due to async timing):
Fetching theme...
Fetching language...
Updating theme to: dark
Updating language to: fr
Fetched: light
Fetched: en
*/