Node.js 7 how to use sequelize transaction with async / await?

前端 未结 6 1212
没有蜡笔的小新
没有蜡笔的小新 2021-01-29 21:43

Node.js 7 and up already support async/await syntax. How should I use async/await with sequelize transactions?

6条回答
  •  庸人自扰
    2021-01-29 22:06

    async () => {
      let t;
    
      try {
        t = await sequelize.transaction({ autocommit: true});
    
        let _user = await User.create({}, {t});
    
        let _userInfo = await UserInfo.create({}, {t});
    
        t.afterCommit((t) => {
          _user.setUserInfo(_userInfo);
          // other logic
        });
      } catch (err) {
        throw err;
      }
    }
    

提交回复
热议问题