Trying to implement a SIMPLE promise in Reactjs
<p>Just trying out Promises for the first time in React. I have a basic promise working (ripped from someone else's code), but don't know how to adapt it to be useful.</p>

<p>What I have so far (within my <code>render()</code> function)</p>

<pre><code>  var promise = new Promise( (resolve, reject) =&gt; {

     let name = 'Dave'

     if (name === 'Dave') {
        resolve("Promise resolved successfully");
     }
     else {
        reject(Error("Promise rejected"));
     }
  });

  promise.then(function(result) {
     console.log(result); // "Promise resolved successfully"
  }, err =&gt; {
     console.log(err); // Error: "Promise rejected"
  });
</code></pre>

<p>Sure enough, as the promise conditional is matched (<code>name === 'Dave'</code>), my console logs <code>Promise resolved successfully</code>. </p>

<p>However, I don't know how to assign a value to a variable when using the promise. For example:</p>

<pre><code>  promise.then(function(result) {
     var newName = 'Bob'
  }, function(err) {
     var newName = 'Anonymous'
  });
</code></pre>

<p>And then when I try to return this value in <code>render()</code>'s return statement, like so:</p>

<pre><code>&lt;h2&gt;{newName}&lt;/h2&gt;
</code></pre>

<p>It says newName is undefined.</p>

<p>I have also tried:</p>

<pre><code>  promise.then(function(result) {
     var newName = result
  }, function(err) {
     var newName = error
  });
</code></pre>

<p>...expecting this would assign the <code>resolve</code> or <code>error</code> string into <code>newName</code> variable, but nope.</p>

<p>Am I thinking about this the wrong way? How can I make this more useful than just logging a string when my conditional is met?</p>

<p>Any help would be appreciated, because this is really making my head hurt...</p>

<hr>

<h2>Update</h2>

<pre><code> render() {

      var promise = new Promise( (resolve, reject) =&gt; {

         let name = 'Paul'

         if (name === 'Paul') {
            resolve("Promise resolved successfully");
         }
         else {
            reject(Error("Promise rejected"));
         }
      });

      let obj = {newName: ''};

      promise.then( result =&gt; {
         obj["newName"] = result
      }, function(error) {
         obj["newName"] = error
      });

      console.log(obj.newName)

    return (
      &lt;div className="App"&gt;
         &lt;h1&gt;Hello World&lt;/h1&gt;
         &lt;h2&gt;{obj.newName}&lt;/h2&gt;
      &lt;/div&gt;
    );
  }
</code></pre>