Browse Source

Multipart upload() now returns promise correctly.

Harry Hedger 9 years ago
parent
commit
53239ff048
2 changed files with 39 additions and 30 deletions
  1. 8 2
      src/core/Core.js
  2. 31 28
      src/plugins/Multipart.js

+ 8 - 2
src/core/Core.js

@@ -169,8 +169,14 @@ export default class Core {
         .map(type => this.runType.bind(this, type, method))
         .map(type => this.runType.bind(this, type, method))
       // Run waterfall of typeMethods
       // Run waterfall of typeMethods
       return Utils.promiseWaterfall(typeMethods)
       return Utils.promiseWaterfall(typeMethods)
-        .then(result => result)
-        .catch(error => console.error(error))
+        .then(result => {
+          if (result[0] !== undefined) {
+            this.log(result)
+            this.log('Upload result -> success!')
+            return result
+          }
+        })
+        .catch(error => this.log('Upload result -> failed:', error))
     })
     })
   }
   }
 }
 }

+ 31 - 28
src/plugins/Multipart.js

@@ -36,40 +36,43 @@ export default class Multipart extends Plugin {
   }
   }
 
 
   upload (files, current, total) {
   upload (files, current, total) {
-    var formPost = new FormData()
+    return new Promise((resolve, reject) => {
+      var formPost = new FormData()
 
 
-    // turn file into an array so we can use bundle
-    if (!this.opts.bundle) {
-      files = [files[current]]
-    }
+      // turn file into an array so we can use bundle
+      if (!this.opts.bundle) {
+        files = [files[current]]
+      }
 
 
-    for (let i in files) {
-      formPost.append(this.opts.fieldName, files[i])
-    }
+      for (let i in files) {
+        formPost.append(this.opts.fieldName, files[i])
+      }
 
 
-    var xhr = new XMLHttpRequest()
-    xhr.open('POST', this.opts.endpoint, true)
+      var xhr = new XMLHttpRequest()
+      xhr.open('POST', this.opts.endpoint, true)
 
 
-    xhr.addEventListener('progress', (e) => {
-      var percentage = (e.loaded / e.total * 100).toFixed(2)
-      this.core.log(percentage)
-      // this.setProgress(percentage, current, total)
-    })
+      xhr.addEventListener('progress', (e) => {
+        var percentage = (e.loaded / e.total * 100).toFixed(2)
+        this.core.log(percentage)
+        // this.setProgress(percentage, current, total)
+      })
 
 
-    xhr.addEventListener('load', () => {
-      var upload = {}
-      if (this.opts.bundle) {
-        upload = {files: files}
-      } else {
-        upload = {file: files[current]}
-      }
-      return Promise.resolve(upload)
-    })
+      xhr.addEventListener('load', () => {
+        var upload = {}
+        if (this.opts.bundle) {
+          upload = {files: files}
+        } else {
+          upload = {file: files[current]}
+        }
 
 
-    xhr.addEventListener('error', () => {
-      return Promise.reject('fucking error!')
-    })
+        return resolve(upload)
+      })
 
 
-    xhr.send(formPost)
+      xhr.addEventListener('error', () => {
+        return reject('fucking error!')
+      })
+
+      xhr.send(formPost)
+    })
   }
   }
 }
 }