dom-examples
dom-examples copied to clipboard
Is async required to make it work?
const` shareData = {
title: 'MDN',
text: 'Learn web development on MDN!',
url: 'https://developer.mozilla.org'
}
const btn = document.querySelector('button');
const resultPara = document.querySelector('.result');
// Share must be triggered by "user activation"
btn.addEventListener('click', async () => {
try {
await navigator.share(shareData)
resultPara.textContent = 'MDN shared successfully'
} catch(err) {
resultPara.textContent = 'Error: ' + err
}
});
Is async function required?
Yes and no. navigator.share() returns a promise. So either you use async/await to wait for the promise to be fulfilled, or you deal with the promise at the lower level.
See https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Asynchronous/Async_await.
Closing, as the OP's question was answered.