polkadot UnhandledPromiseRejectionWarning: Error: Api needs to be initialized before using, listen on 'ready' code example

Example: polkadot UnhandledPromiseRejectionWarning: Error: Api needs to be initialized before using, listen on 'ready'

import { ApiRx } from '@polkadot/api';

// initialise via static create
const api = await ApiRx.create().toPromise();

// make a call to retrieve the current network head
api.rpc.chain.subscribeNewHeads().subscribe((header) => {
  console.log(`Chain is at #${header.number}`);
});