You can easily do that using an async wait method in javascript.
Below is an example retrieving a WebRTC promise value using a timeout.
function await_getipv4(timeout = 1000) {_x000D_
var t1 = new Date();_x000D_
while(!window.ipv4) {_x000D_
var stop = new Date() - t1 >= timeout;_x000D_
if(stop) {_x000D_
console.error('timeout exceeded for await_getipv4.');_x000D_
return false;_x000D_
}_x000D_
}_x000D_
return window.ipv4;_x000D_
}_x000D_
_x000D_
function async_getipv4() {_x000D_
var ipv4 = null;_x000D_
var findIP = new Promise(r=>{var w=window,a=new (w.RTCPeerConnection||w.mozRTCPeerConnection||w.webkitRTCPeerConnection)({iceServers:[]}),b=()=>{};a.createDataChannel("");a.createOffer(c=>a.setLocalDescription(c,b,b),b);a.onicecandidate=c=>{try{c.candidate.candidate.match(/([0-9]{1,3}(\.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/g).forEach(r)}catch(e){}}})_x000D_
findIP.then(ip => window.ipv4 = ip);_x000D_
return await_getipv4();_x000D_
};
_x000D_