Skip to content

Instantly share code, notes, and snippets.

@davidcallanan
Created December 4, 2022 17:24
Show Gist options
  • Save davidcallanan/606c9d5293118b594a379bc497a52a68 to your computer and use it in GitHub Desktop.
Save davidcallanan/606c9d5293118b594a379bc497a52a68 to your computer and use it in GitHub Desktop.
const timeout = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
const server_side_call = async (method, ...args) => {
if (!["foo", "bar", "baz"].includes(method)) {
throw new Error(`invalid server-side method ${method}`);
}
console.log(`${method} successfully executed server-side`);
};
const create_api = async () => {
let api = new Proxy({
foo: async (...args) => {
console.log(`foo successfully executed client-side`);
},
}, {
get: (target, name) => {
if (target.hasOwnProperty(name)) {
return target[name];
}
return async (...args) => {
return await server_side_call(name, ...args);
};
},
});
return timeout(100).then(() => api);
};
(async () => {
await timeout(10);
const api = await create_api();
await timeout(10);
api.foo();
await timeout(10);
api.bar();
await timeout(10);
api.baz();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment