登陆

javascript - ES7 async和await方法使用问题

想利用await关键字把返回的url赋给url变量
但是console.log一直打出undefined

uploadImgReq方法代码如下:

uploadImgReq: function(file) {
                var nameKey = this.generateUUID();
                // event.target.files[0].name = nameKey;
                this.$http.get(this.url + '/qiniu/token', {
                    params: {
                        key: nameKey
                    }
                }).then((response) => {
                    this.token = response.data.data.token;
                    if (this.token != null) {
                        var formData = new FormData();
                        formData.append('file', file);
                        formData.append('key', nameKey);
                        formData.append('token', this.token);
                        this.$http.post('http://upload.qiniu.com/', formData, {
                            headers: {
                                'Content-Type': 'multipart/form-data'
                            }
                        }).then((response) => {
                            if (response.status == 200) {
                                this.uploadStatus = false;
                                this.$message({
                                    message: '上传成功',
                                    type: 'success'
                                });
    
                                var imgUrl = this.qiniu + response.body.key;
                                console.log("i am returned")
                                return new Promise(function (resolve, reject) {
                                    resolve(imgUrl)
                                })
                            }
                        }).catch((response) => {
    
                        })
                    }
                }).catch((response) => {
    
                })

请问各位问题出在哪里了呢

# JavaScript
三叔三叔1593 天前366 次浏览

全部回复(2)我要回复

  • 女神的闺蜜爱上我

    女神的闺蜜爱上我2017-06-15 09:24:50

    this.$http.post('http://upload.qiniu.com/', formData, {
        headers: {
            'Content-Type': 'multipart/form-data'
        }
    })

    改为

    return this.$http.post('http://upload.qiniu.com/', formData, {
        headers: {
            'Content-Type': 'multipart/form-data'
        }
    })

    回复
    0
  • 女神的闺蜜爱上我

    女神的闺蜜爱上我2017-06-15 09:24:50

    uploadImgReq里的promise没有return

    uploadImgReq: function(file) {
    ......
    return this.$http.get(this.url + '/qiniu/token', { //没有return
    ......
    }

    另外promise.then里不应该嵌套promise,应该return promise,然后用then链式调用的方式来

    回复
    0
  • 取消回复发送