r/JavaScriptTips • • Jan 13 '23

Copying text to clipboard and handling result with async/await 📋

Post image
12 Upvotes

1 comment sorted by

2

u/Low_Mammoth_9371 Jan 13 '23

Async copy function:

async function readClipboard() {
    try {
        await navigator.clipboard.readText();
        alert("Success! Read: " + res)
    } catch {
        alert("Failed to read the clipboard: " + err);
    }
}

readClipboard("text-to-copy");