Возврат значения ответа от axios снаружи - PullRequest
0 голосов
/ 16 января 2019

Мне нужно передать параметр для разрешения новых Обещаний. Данные, которые мне нужно передать, взяты из ответа axios, вот мой код axios

async function uploadFile() {
const {value: file} = await Swal({
      title: 'Select image',
      input: 'file',
      inputAttributes: {
        'accept': 'image/*',
        'aria-label': 'Upload your profile picture'
      }
    })

    if (file) {
      const reader = new FileReader
      reader.onload = (e) => {
        Swal({
            title: 'Your uploaded picture, Do you want to upload it?',
              text: "Ypur photo will be uploaded!",
              type: 'info',
              imageUrl: e.target.result,
              showCancelButton: true,
              confirmButtonColor: '#3085d6',
              cancelButtonColor: '#d33',
              confirmButtonText: 'Yes, delete it!'
        }).then((result) => {
            if(result.value) {
                console.log(file, 'file')
                let formData = new FormData;
                formData.append('photo', file);
                let settings = { headers: { 'Content-Type': 'multipart/form-data'}}
                axios.post('/agent_dashboard/save_photo', formData)
                .then((res) => {
                    const vl = res.directory => this i want to use
                    return vl
                })
                .catch((err) => {
                    console.log(err.toString(), 'error')
                })
            }
        })
      }
      reader.readAsDataURL(file)
    }
}

Вот моя другая функция, для которой требуется объект ответа axios

      ngjs.on('browse', () => {
         return new Promise((resolve, reject) => {
          try   {
             const value = uploadFile()

            resolve(value)
        } catch(e) {
            reject(new Error('something failed'))
        }
    })

  })

Когда я запускаю это, все, что у меня есть в консоли, это

   Promise {<pending>}
   [[PromiseStatus]]: "resolved"
   [[PromiseValue]]: undefined

Есть идеи?

1 Ответ

0 голосов
/ 16 января 2019

Возможно, в этом блоке кода:

      ngjs.on('browse', () => {
         return new Promise((resolve, reject) => {
          try   {
             const value = uploadFile()

            resolve(value)
        } catch(e) {
            reject(new Error('something failed'))
        }
    })

  })

Попробуйте добавить 'await' перед uploadFile (), то есть:

const value = await uploadFile()
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...