Skip to content

Instantly share code, notes, and snippets.

@jameswomack
Created December 3, 2017 09:51
Show Gist options
  • Select an option

  • Save jameswomack/85221671986f29246afda500ee630b68 to your computer and use it in GitHub Desktop.

Select an option

Save jameswomack/85221671986f29246afda500ee630b68 to your computer and use it in GitHub Desktop.
Async Squared: You know that we are developing in a ternary world & I am a ternary girl
const Shareability = { ofURI() { return Promise.resolve(location.href) }}
const canShare = ({ canonical = false } = { }) => {
if (!navigator.share)
return Promise.resolve(false)
return Shareability.ofURI(canonical?document.querySelector('link[rel="canonical"]'):location.href)
}
const beSelfish = () =>
Promise.reject(new Error('Sharing unavailable'))
const shareAttempt = async ({ title, text }, url) =>
await (url = await canShare({ canonical: true }) ?
navigator.share({ title, text, url }) :
beSelfish())
// async () => { try { await shareAttempt({ title:'Async/Await', text: 'They\'re so cool!' }) } catch (e) { console.error(e) // Internal error: could not connect to Web Share interface. } }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment