Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 12111

Block JavaScript operations until a promise is resolved [duplicate]

$
0
0

I am trying to fetch a file into a synchronous class. But I cannot solve a problem that appeared: I need to get a return value of a function and store it into a variable, although I do not know when the promise will be fulfilled. Can someone explain a way to get my body?

function fetchSource(name: string): string {    let source: string | null;    getFileContents(name).then((response) => source = response);    if (!source) { // <-- here we do not know at runtime what the value of this varaible is        throw new Error("Cannot fetch shader source file " + name +".");    }    return source;}

Viewing all articles
Browse latest Browse all 12111

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>