Skip to content

Instantly share code, notes, and snippets.

@negue
Created November 29, 2019 20:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save negue/857c98d5b1273ad8334bb5075fda4feb to your computer and use it in GitHub Desktop.
Save negue/857c98d5b1273ad8334bb5075fda4feb to your computer and use it in GitHub Desktop.
gun call-/fallback
private callFallback<T> (functionName: 'once'|'load', timeout: number, defaultValue: T): Promise<T> {
return new Promise(resolve => {
let timeoutCalled = false;
const stopTimeout = setTimeout(() => {
timeoutCalled = true;
this.zone.run(() => resolve(defaultValue));
}, timeout);
this.gun[functionName](data => {
if (timeoutCalled) {
return;
} else {
clearTimeout(stopTimeout);
}
this.zone.run(() => resolve(data as T));
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment