Home > Back-end >  Promises: I would like to use the return value of a promise to create an instance and export instanc
Promises: I would like to use the return value of a promise to create an instance and export instanc

Time:08-24

I would like to assign the instance of PusherPushNotifications.Client into a variable beamsClient and export the module. PusherPushNotifications.Client requires the serviceWorkerRegistration as part of the options

inside pusher.js, I have the following but beamsClient:

import * as PusherPushNotifications from '@pusher/push-notifications-web'

export default function tokenProvider() {
  return new PusherPushNotifications.TokenProvider({
    url: 'http://test-backend.com/beams-auth',
    queryParams: {
    },
    headers: {
      Authorization: 'FooBarBazToken'
    }
  })
}

const init = async () => {
  await window.navigator.serviceWorker.ready.then(serviceWorkerRegistration => {
    beamsClient = new PusherPushNotifications.Client({
      instanceId: 'INSTANCE_ID',
      primaryKey: 'PRIMARY_KEY',
      serviceWorkerRegistration
    })
  })
}

// ------->>>  I need the instance of Client here
let beamsClient = init()

console.log('Beams', beamsClient)

export { beamsClient }


CodePudding user response:

You cannot export a promise result. You could, though, export the promise and have whatever's importing the file await it from there.

  • Related