【问题标题】:How can I use promise.all using mysql insert to cascade style?如何使用promise.all 使用mysql 插入到级联样式?
【发布时间】:2018-12-16 02:01:59
【问题描述】:

我想使用 Promise 插入数据 3 表,但我遇到了问题。我想在任务表中插入数据,获取结果插入 id,将该 id 插入其他表并获取此插入 id,将该 id 插入其他表。我想使用 Promise.all 函数来做到这一点。这是我的代码。

addClientTable(insertTask)
    .then(client_res => {
        return client_res.insertId;
    })
    .then(clientInsId => {
        insertTask.task.client_id = clientInsId;
        addTaskTable(insertTask)
            .then(task_res => {
                if (insertTask.task.connection == 0) {
                    updateTaskConnection(task_res.insertId)
                        .then(res => {
                            console.log(res);
                        })
                        .catch(err => {
                            console.log(err);
                        })
                }
                return task_res.insertId;
            })
            .then(taskInsId => {

                addTaskUsersTable(user_ids, taskInsId, clientInsId)
                    .then(result => {
                        resolve(result);
                    })
            })
            .catch(err => {
                reject(err);

            })
    })
    .catch(err => {
        reject(err);
    });

【问题讨论】:

  • Promise.all 假设任务可以完全独立运行。由于您以后的承诺依赖于早期承诺的 id 值,因此.then() 链接更适合您的需求。
  • 您可以通过链接then()s 来完成这一切,但是,async/await 可能会使一切变得更清晰,更易于遵循..
  • Mark Meyer 我不知道怎么做。如果你知道,请为我写代码示例。谢谢

标签: javascript mysql node.js promise es6-promise


【解决方案1】:

我不认为需要Promise.all,但为了更简洁的代码并避免嵌套.then(),我建议async/await,承诺的含糖语法,你的代码应该看起来像:

async function myFn (){
  const clientInsId = await addClientTable(insertTask).insertId;

  insertTask.task.client_id = clientInsId;

  const task_res = await addTaskTable(insertTask);
  let res;

  if (insertTask.task.connection == 0) {
    res = await updateTaskConnection(task_res.insertId);
    console.log(res);
  }

  const result = await addTaskUsersTable(user_ids, taskInsId, clientInsId);

  return result;
};

myFn()
  .then(data => console.log(data))
  .catch(err => console.log(err))

【讨论】:

  • 您可以使用一个Promise.all,用于addTaskUsersTableupdateTaskConnection(如果运行第二个)。只需使用addTaskUsersTable 调用创建一个数组,然后为updateTaskConnection 创建一个if...array.push(...),然后是let [result, res] = await Promise.all(array);
【解决方案2】:

任何来自承诺的回报都是可以的。

基于此,您还可以将代码修改为如下所示。

addClientTable(insertTask)
    .then(client_res => {
        return client_res.insertId;
    })
    .then(clientInsId => {
        insertTask.task.client_id = clientInsId;
        return addTaskTable(insertTask)
    })
    .then(task_res => {
        if (insertTask.task.connection == 0) {
            updateTaskConnection(task_res.insertId)
                .then(res => {
                    console.log(res);
                })
                .catch(err => {
                    console.log(err);
                })
        }
        return task_res.insertId;
    })
    .then(taskInsId => {
        return addTaskUsersTable(user_ids, taskInsId, clientInsId)
    })
    .then(result => {
        resolve(result);
     })
    .catch(err => {
        reject(err);
    });

虽然使用 async/await 是可行的方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-05-15
    • 1970-01-01
    • 1970-01-01
    • 2011-11-23
    • 2015-09-25
    • 1970-01-01
    • 2018-07-08
    相关资源
    最近更新 更多