Created
December 4, 2022 17:23
-
-
Save davidcallanan/e99b4c2388b967656ab0f471228d0074 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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