【问题标题】:Vue: Turning method into promise?Vue:将方法变成承诺?
【发布时间】:2020-05-26 04:24:51
【问题描述】:

我正在使用 Dropzone 来处理图像。我在单击按钮时手动调用图像处理,所以在一个名为 submitAssets() 的函数中,我正在这样做:

submitAssets: function() {
   this.$refs.dropzoneReference.processQueue();
   axios.get('/users')
}

dropzone 中的 processQueue 调用的一部分命中我的后端并将图像 url 保存到用户对象。
问题是 axios 调用从用户对象获取信息,但图像尚未处理并保存到数据库。有没有办法可以将其设为 Promise,以便在 processQueue 完成之前不会调用 axios 调用?

【问题讨论】:

  • 如果需要回调,您可以使用 new Promise() 将其转换为 Promise。
  • processQueue 已经返回一个 Promise 了吗?还是怎么定义的?
  • 使用异步等待方法
  • 使用dropzone events 处理您的具体情况可能会有所帮助。如果您希望 axios 在所有上传完成后触发,也许queuecomplete

标签: javascript vue.js dropzone.js


【解决方案1】:

如果processQueue 已经返回了一个承诺,那么你可以使用async/await。否则,您将需要将其转化为承诺,以使以下内容起作用。

submitAssets: async function() {
   await this.$refs.dropzoneReference.processQueue();
   axios.get('/users')
}

processQueue 中,您可以执行以下操作:

const processQueue = () => {
  return new Promise((resolve, reject) => {
  ...make your changes
  resolve();
  });
}

【讨论】:

  • processQueue 是 Vue Dropzone 库中的一个方法。我只是查看了文档,它没有返回 Promise。
猜你喜欢
  • 2017-02-15
  • 1970-01-01
  • 1970-01-01
  • 2017-10-09
  • 2019-04-07
  • 2017-06-18
  • 2020-04-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多