Problem: Proxy a Remote Object Over Network Simulation
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
getandsettraps.Simulate network latency using
setTimeoutandPromise.The Proxy should not store state locally—each access or write should trigger a new simulated network call.
Reading a property should return a
Promisethat 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 propertiesremoteConfig.theme = 'dark';remoteConfig.language = 'fr';/* Expected output (order may vary due to async timing):Fetching theme...Fetching language...Updating theme to: darkUpdating language to: frFetched: lightFetched: 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
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
getandsettraps.Simulate network latency using
setTimeoutandPromise.The Proxy should not store state locally—each access or write should trigger a new simulated network call.
Reading a property should return a
Promisethat 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 propertiesremoteConfig.theme = 'dark';remoteConfig.language = 'fr';/* Expected output (order may vary due to async timing):Fetching theme...Fetching language...Updating theme to: darkUpdating language to: frFetched: lightFetched: en*/
Good luck trying the problem! If you’re unsure how to proceed, check the “Solution” tab above.