title: Javascript Promise Chaining Issues (Ember)
body: <p>I'm trying to upload a file to a remote server, get the path for that file on that server, and then save it in a record to my own API. Over half the time, the code crashes because returnedNewFile is undefined (though I've checked console.log(newFile) in the previous .then() function and it's always a promise object. What could be causing this behavior? What could be causing returnedNewFile to sometimes be null/undefined? I'm using Django REST + django-rest-json-api + ember.js 2.6, and this code is in a component's javascript file. Thank you in advance!</p>
  
  <pre><code>  store.findRecord('file', folderid).then(function(folder) {
      var file = fileList.pop();
      var newFile = fm.uploadFile(folder, file.name, file);
      return newFile;
  
    }).then(function(returnedNewFile) {
      var name = returnedNewFile.get('name');
      var path = returnedNewFile.get('path');
      var doc = store.createRecord('document', {
        name: name,
        path: path,
      });
      console.log("doc: ", doc);
      doc.save();
      return doc;
  
    }).then(function(doc) {
      console.log("hi number three");
      var grant = store.createRecord('grant', {
        department: department,
        number: number,
        document: doc,
  
      });
      grant.save();
      return grant;
  
    }).then(null, function(error) {
      console.log("Oops: " + error.message)
    });
  </code></pre>
  
  <p>EDIT: Here's the code for <code>uploadFile()</code> (waterbutler is the name of the file server being used on the external server (<a href="https://github.com/CenterForOpenScience/waterbutler" rel="nofollow">link</a>)):</p>
  
  <pre><code>uploadFile(folder, name, contents, options = {}) {
      let url = folder.get('links').upload;
      options.data = contents;
      if (!options.query) {
          options.query = {};
      }
      options.query.name = name;
      options.query.kind = 'file';
  
      let p = this._waterbutlerRequest('PUT', url, options);
      return p.then(() =&gt; this._getNewFileInfo(folder, name));
  }
  </code></pre>
  
  <p>And here's the code for _getNewFileInfo:</p>
  
  <pre><code>/**
   * Get the `file` model for a newly created file.
   *
   * @method _getNewFileInfo
   * @private
   * @param {file} parentFolder Model for the new file's parent folder.
   * @param {String} name Name of the new file.
   * @return {Promise} Promise that resolves to the new file's model or
   * rejects with an error message.
   */
  _getNewFileInfo(parentFolder, name) {
      let p = this._reloadModel(parentFolder.get('files'));
      return p.then((files) =&gt; files.findBy('name', name));
  }
  </code></pre>