Created
November 29, 2019 20:46
-
-
Save negue/857c98d5b1273ad8334bb5075fda4feb to your computer and use it in GitHub Desktop.
gun call-/fallback
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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