让数据等待Javascript上变量finish的声明?

让数据等待Javascript上变量finish的声明?,javascript,json,vue.js,Javascript,Json,Vue.js,我有以下代码。我要做的是等待值完成,然后再执行分派。 发生的情况是,它调度并只能获取第一个值,而不能获取下面的foreach值 提前谢谢 syncPO() { this.wait = true this.$store.dispatch('account/checkToken', this.optionValues.syncValue).then((res) => { this.wait = 'stepTwo' this.max = res.lengt

我有以下代码。我要做的是等待值完成,然后再执行分派。 发生的情况是,它调度并只能获取第一个值,而不能获取下面的foreach值

提前谢谢

syncPO() {
    this.wait = true
    this.$store.dispatch('account/checkToken', this.optionValues.syncValue).then((res) => {
      this.wait = 'stepTwo'
      this.max = res.length
      res.forEach(result => {
            this.POValues.po_number = result.DocNumber
            this.POValues.vendor_id = result.VendorRef
            this.POValues.order_date=result.MetaData.CreateTime
            this.POValues.amount=result.TotalAmt
            this.POValues.currency=result.CurrencyRef
            this.POValues.qb_poId=result.Id
            this.POValues.status=result.POStatus
            this.POValues.line = result.Line
              this.$store.dispatch('purchaseOrder/createPO',this.POValues).then(res=>{
                  // console.log(this.POValues.line)
                this.value += 1;
              }).then((res)=>{
                this.$store.dispatch('purchaseOrder/create-lists')
              })
        })
      })

  },

这里有一些东西

您需要将函数声明为类似异步的

async syncPO(){  ...
等待调度,而不是等待。语法如下

await this.$store.dispatch(
            'account/checkToken', this.optionValues.syncValue
        ).then((res) => {  ...
为了让分派的操作返回您正在等待的承诺,操作必须是var,而不是const

export var actions = { ...

如果您使用的是axios,您的数据应该是
res.data
,而不是
res

这里有一些东西

您需要将函数声明为类似异步的

async syncPO(){  ...
等待调度,而不是等待。语法如下

await this.$store.dispatch(
            'account/checkToken', this.optionValues.syncValue
        ).then((res) => {  ...
为了让分派的操作返回您正在等待的承诺,操作必须是var,而不是const

export var actions = { ...
如果您使用的是axios,您的数据应该是
res.data
,而不是
res

尝试使用承诺。试着用承诺。