Skip to content

Instantly share code, notes, and snippets.

@oliverturner
Last active January 24, 2018 17:17
Show Gist options
  • Save oliverturner/3a3f3f8f3856279779b297a8ff2c6791 to your computer and use it in GitHub Desktop.
Save oliverturner/3a3f3f8f3856279779b297a8ff2c6791 to your computer and use it in GitHub Desktop.
async function structuredClone(obj) {
return new Promise(resolve => {
const { port1, port2 } = new MessageChannel();
port2.onmessage = evt => resolve(evt.data);
port1.postMessage(obj);
});
}
// Usage
const a = { a: 1, b: 2, c: [1, 2, 3], d: { person: { name: "oliver" } } };
const { d } = await structuredClone(a);
console.log(d); // <= {person: {name:"oliver"}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment