InvalidStateError: Cannot create offer in state have-remote-offer

My starting point.

by jib1

HTML

<div id="div" style="white-space: pre;"></div>

JavaScript

const console = { log: msg => div.innerHTML += msg + "<br>" };
const wait = ms => new Promise(resolve => setTimeout(resolve, ms));
const pc = new RTCPeerConnection();

(async () => {
  try {
    const offer = await pc.createOffer();
    pc.addTransceiver("video");
    pc.onnegotiationneeded = async () => {
      (async () => {
        try {
          await pc.setLocalDescription();
        } catch (e) {
          console.log(`SLD fail: ${e}`);
        }
      })();
      try {
        await wait(0);
        await pc.setRemoteDescription(offer);
        console.log("hi");
      } catch(e) {
        console.log(e);
      }
    };
  } catch(e) {
    console.log(e);
  }
})();