Skip to content

Instantly share code, notes, and snippets.

@thadeu
Last active July 20, 2018 07:42
Show Gist options
  • Save thadeu/3b084322fe1f0362b4cd2ce17a7d89e2 to your computer and use it in GitHub Desktop.
Save thadeu/3b084322fe1f0362b4cd2ce17a7d89e2 to your computer and use it in GitHub Desktop.
Promise Reject Delayed
export const getNotifications = async (title) => {
  let ASYNC_TIMEOUT = 10000 // 10s
  let didTimeout = false
  let notifications = []

  const workerDelayedPromise = () => {
    return new Promise(async (resolve, reject) => {

      // timeout in order to send reject if delayed
      const timeout = setTimeout(() => {
        didTimeout = true
        reject('Request notifications timed out')
      }, ASYNC_TIMEOUT)

      // clearTimeout and didTimeout to false
      const cleanTimeout = () => {
        clearTimeout(timeout)
        didTimeout = false
      }

      // get current serviceWorker
      let worker = await serviceWorker

      try {
        // get notifications was open in the navigator
        let response = await worker.getNotifications()

        // running only if timeout(ASYNC_TIMEOUT) still exists
        cleanTimeout()
        
        if (!didTimeout){
          resolve(response)
        }
      } catch (error) {
        reject(error)
      }
    })
  }

  try {
    notifications = await workerDelayedPromise()
    
    if (notifications.length <= 0) return []
  
    if (title){
      return notifications.filter(item => {
        if (item.title == title){
          return item
        }
      })
    }
  
    return notifications
  } catch (error) {
    console.error(error)
    return []
  }
}
@WebCloud
Copy link

Here's a bit of code review for ya 😄

const ASYNC_TIMEOUT = 10000 // 10s
let didTimeout = false

// no need to re-create on each run
// clearTimeout and didTimeout to false
const cleanTimeout = (timeout) => {
  clearTimeout(timeout)
  didTimeout = false
}

// No need to create the function every run
const workerDelayedPromise = () => {
  return new Promise(async (resolve, reject) => {
    didTimeout = false // reset for re-runs

    // timeout in order to send reject if delayed
    const timeout = setTimeout(() => {
      didTimeout = true
      reject('Request notifications timed out')
    }, ASYNC_TIMEOUT)

    // get current serviceWorker
    const worker = await serviceWorker

    // technically speaking you don't really need a try/catch
    // because throwing will reject promise with the error
    // just like you are manually doing
    // try {
      // get notifications was open in the navigator
    const response = await worker.getNotifications()

    // running only if timeout(ASYNC_TIMEOUT) still exists
    cleanTimeout(timeout)

    // no need for the check
    // if it does timeout it won't reach here
    // for the promise will be rejected
    // if (!didTimeout) {
    //   resolve(response)
    // }

    resolve(response)
    // } catch (error) {
    //   reject(error)
    // }
  })
}

export const getNotifications = async (title) => {
  let notifications = []

  try {
    notifications = await workerDelayedPromise()
    
    // no need for this if
    // notifications will never < 0
    // also filter of empty array is a NOOP which in turn will return
    // notificatiions default value, []
    // if (notifications.length === 0) return []
  
    if (title){
      // Array.prototype.filter (or find and findIndex) works with a boolean check, not value return, that's for map/reduce
      return notifications.filter(item => item.title === title)
    }
  
    return notifications
  } catch (error) {
    console.error(error)
    // you can just return notifications since it is an empty array
    return notifications
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment