MCPcopy Create free account
hub / github.com/codecombat/codecombat / watchForPageUnload

Function watchForPageUnload

app/core/Tracker2/pageUnload.js:34–48  ·  view source on GitHub ↗
(timeout = 500)

Source from the content-addressed store, hash-verified

32}
33
34export async function watchForPageUnload (timeout = 500) {
35 let unloadCallback
36 const unloadPromise = new Promise((resolve, reject) => {
37 unloadCallback = () => reject('unload') // eslint-disable-line prefer-promise-reject-errors
38 window.addEventListener('beforeunload', unloadCallback)
39 })
40
41 const timeoutPromise = new Promise((resolve) => setTimeout(resolve, timeout))
42
43 try {
44 await Promise.race([unloadPromise, timeoutPromise])
45 } finally {
46 window.removeEventListener('beforeunload', unloadCallback)
47 }
48}
49
50/**
51 * Monitors the page for unload events for a specified timeout after calling the supplied function.

Callers 1

retryOnPageUnloadFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected