get object promise in a function that first gets clipboard value, then sets it to local storage in javascript

To get the clipboard value in JavaScript, we need to use the Clipboard API. Once we get the value, we can store it in local storage. As both of these operations are asynchronous, we can use Promises to make sure that we get the results in order.

Here is an example function that retrieves the clipboard value and stores it in local storage:

index.tsx
function getClipboardValueAndStoreLocalStorage() {
  return new Promise((resolve, reject) => {
    navigator.clipboard.readText()
      .then(value => {
        localStorage.setItem('clipboardValue', value);
        resolve(value);
      })
      .catch(error => reject(error));
  });
}
287 chars
11 lines

This function returns a Promise that resolves with the clipboard value if it is successfully retrieved and stored in local storage. If either operation fails, the Promise will reject with an error.

To use this function, you can call it like this:

index.tsx
getClipboardValueAndStoreLocalStorage()
  .then(value => console.log(`Clipboard value: ${value}`))
  .catch(error => console.error(error));
140 chars
4 lines

This will log the clipboard value to the console if the Promise resolves successfully, or log an error message if it rejects.

gistlibby LogSnag