title: I would like to use a variable outside of a function with fetch in Javascript
body: <p>I would like to use a function then return a value back. This function was written in a script in an external Javascript called &quot;tools_4.js&quot;.</p>
  <p>Here ist the code of the tools_4.js:</p>
  <pre><code>function sendIso3Info(inCurrency) {
    let iso3Info = {
      'text': inCurrency,
    }
  
    einenWert = 'Hallo';
  
    fetch(`/processIso3Info/${JSON.stringify(iso3Info)}`)
      .then(function(response) {
        return response.text();
      })
      .then(function(text) {
        console.log('GET response text:');
        einenWert = text;
        console.log('This is the value in the fetch')
        console.log(einenWert);
  
      })
      .then(function(text) {
        data_function(einenWert); //calling and passing to another function data_function
      })
  
    //another functions
    function data_function(data) {
      console.log('ich bin in der neuen Funkion!');
      alert(data);
      temp = data;
      console.log(temp);
    }
  
    console.log('Value outside of the function');
    console.log(temp);
  
    return temp;
  }
  </code></pre>
  <p>I was using the fetch-function to become the value. But I can not use the variable &quot;temp&quot;, because it's written &quot;undefined&quot;. I was trying with a global variable, but it doesn't work?</p>
  <p>here is the code of the app.py:</p>
  <pre><code>########  imports  ##########
  from flask import Flask, jsonify, request, render_template
  import currencys
  import json
  
  app = Flask(__name__)
  
  
  @app.route('/')
  def home_page():
      example_embed='This string is from python'
      return render_template('index.html', embed=example_embed)
  
      
  ######## Data fetch ############
  @app.route('/processIso3Info/&lt;string:iso3Info&gt;', methods=['GET','POST'])
  def processIso3(iso3Info): 
      if request.method == 'POST': # POST request
          print(request.get_text())  # parse as text
          return 'OK', 200
      else: # GET request
          iso3InfoCurrency = json.loads(iso3Info) 
          out = currencys.return_iso3(iso3InfoCurrency['text'])
      
          return out
  
  app.run(debug=True)
  
  </code></pre>
  <p>and the code of the template:</p>
  <pre><code>&lt;head&gt;
      
  
  &lt;/head&gt;
      
  &lt;body&gt;
  
      &lt;h1&gt; Python Fetch Example&lt;/h1&gt;
      &lt;p id='embed'&gt;{{embed}}&lt;/p&gt;
      &lt;p id='mylog'/&gt;
      &lt;script type=&quot;text/javascript&quot; src=&quot;{{ url_for('static', filename='tools_4.js') }} 
      &lt;/script&gt;
      &lt;script&gt; 
          iso3Text = sendIso3Info('Euro');
      &lt;/script&gt;
      
  &lt;body&gt;
  
  </code></pre>