How to wait for event request of Rudderstack JavaScript SDK?

Viewed 217

I want to redirect the user after the track event or any other event fired by rudderstack has be done.

As I've tested on track event it doesn't not return a promise.

How can I wait for the requests to end to redirect after?

Below there is an example of what i am doing:

// import    
import { track, load, ready, identify } from 'rudder-sdk-js';
[...]

await load(write_key, data_plane_url)

await ready(async () => {
  await identify(hashedId, userInfo);

  await track('ORDER_APPROVED');    
});

history.push('/some-page')

I'm using it with React btw.

1 Answers

You cant use await in root file, it has to be contained in an async Function

Add it in useEffet or componentdidmount Function and handle it from there.

// useEffect inside a tsx or js file 
import {useEffect, useState} from 'react'
const App(){
var [userName, setUserName] = useState("")

const loadUserName= async()=>{

 var us =  await // fetch operation
  
  await setUserName(us);
}

useEffect(()=> {
    loadUserName();
},[]) // componentDidMount

return (<p>{userName}</p>)
}
Related