.then()在先前的.then()返回之前触发

时间:2018-12-17 23:01:41

标签: javascript promise es6-promise

我正在从本地销售点数据库(第三方软件)中获取类别信息,并尝试将其写入WooCommerce商店。我还将数据存储到我自己的数据库中,以维护两个系统之间的关系。我正在使用Promise对所有内容进行排序,但是我的.then()语句之一在前一个.then()返回之前就触发了,因此我向WooCommerce发送了空的有效载荷。

router.post("/:action", (req, res) => {
  if(req.params.action === "sync" && req.body.action === "sync") {
    // Query the POS database
    mssql.query(query, (err, result) => {
      let postData = {
        create: [],
        update: []
      }
      // Make some promises to pull the POS categories and their children
      Promise.all(promises)
        .then(cats => {
          let catPromises = cats.map(cat => {
            return new Promise((resolve, reject) => {
              Category.findOne(
                // Check for existing entry in the linking DB...
              )
                .then(data => {
                  // ...and handle accordingly
                  resolve()
              })
                .then(() => {
                  let childPromises = cat.children.map(child => {
                    return new Promise((resolve, reject) => {
                      Category.findOne(
                        // Checking for existing entry in the linking DB...
                      )
                        .then(data => {
                          // ...and handle accordingly
                          resolve()
                      })
                    })
                  })
                  Promise.all(childPromises)
                    .then(resolved => {
                      resolve()
                  })
              })
            })
          })
          Promise.all(catPromises)
            .then(() => {
              return
          })
      })
        .then(() => {
          // This is the part that's firing early
          return axios.post(
            // data
          )
      })
...

编辑:新重构,仍然有问题。

Promise.all(promises).then(cats => {
  let catPromises = cats.map(cat => {
    Category.findOne(
      // Check for existing...
    ).then(data => {
      // ...and handle accordingly
    }).then(() => {
      let childPromises = cat.children.map(child => {
        Category.findOne(
          // Check for existing...
        ).then(data => {
          // ...and handle accordingly
        })
      })
      return Promise.all(childPromises)
    })
  })
  // Now this is where we're reaching early
  return Promise.all(catPromises)
}).then(() => {
  // API call
})

1 个答案:

答案 0 :(得分:0)

最终解决方案:

Promise.all(promises).then(cats => {
  let catPromises = cats.map(cat => {
    return Category.findOne(
      // Check for existing...
    ).then(data => {
      // ...and handle accordingly
    }).then(() => {
      let childPromises = cat.children.map(child => {
        return Category.findOne(
          // Check for existing...
        ).then(data => {
          // ...and handle accordingly
        })
      })
      return Promise.all(childPromises)
    })
  })
  // Now this is where we're reaching early
  return Promise.all(catPromises)
}).then(() => {
  // API call
})