代码之家  ›  专栏  ›  技术社区  ›  Sameer

使异步代码像同步-javascript一样工作

  •  1
  • Sameer  · 技术社区  · 6 年前

    我在用nodejs

    这是我的名为createSchema的函数:

    const createSchema = () => {
      Business.findAll({
        raw: true,
      }).then((data) => {
        data.forEach((client) => {
          postgresDB.createSchema(client.code).then(() => {
            Object.keys(postgresDB.models).forEach((currentItem) => {
              postgresDB.models[currentItem].schema(client.code).sync();
            });
            console.log('Postgres schema created');
          }).catch(() => {
          });
        });
      }).catch((err) => {
        console.log('Warning:', err.message);
      });
    };
    createSchema();
    

    我在这个post函数中调用这个函数

    exports.createBusiness = (req, res) => {
      const business = {
        name: req.body.name,
        code: req.body.code,
        email: req.body.email,
      };
      Business.create(business)
        .then((rawbusinessData) => {
          createSchema()     // this is the function
            .then(() => { .  // i want to complete createSchema fully then only i want to execute this below stuffs
              const businessData = rawbusinessData.get({ plain: true });
              const loginDetails = {
                username: 'sameer',
                password: encrypt('sameer'),
              };
              const schemaLogin = postgresDB.models.login.schema(businessData.code);
              schemaLogin.create(loginDetails).then((loginData) => {
                console.log('loginData:', loginData);
              });
              res.status(200).send(businessData);
            });
        })
        .catch((err) => {
          console.log('err:', err);
        });
    };
    

    我正在调用第二个post函数中的第一个函数 创建业务 ,

    我想完全完成createSchema函数,然后只需要执行其他 然后 第二个函数中的方法()被调用 创建业务

    看到我的代码,我做了一个需要首先工作的评论,

    我尝试了异步等待,但没有工作!

    2 回复  |  直到 6 年前
        1
  •  2
  •   Neil Lunn    6 年前

    你错过了很多地方的承诺。您需要归还所有这些物品:

    // No "block" implies return
    const createSchema = () =>
      Business.findAll({ raw: true})
        .then((data) => 
          // wrap Promise.all and map() instead of forEach()
          Promise.all(
            data.map((client) =>
              postgresDB.createSchema(client.code).then(() => 
                // Again wrap Promise.all and map()
                Promise.all(
                  Object.keys(postgresDB.models).map((currentItem) => 
                    postgresDB.models[currentItem].schema(client.code).sync()
                  )
                )
              )
            )
          )
        )
        .then(() => console.log("now I'm done"))
        //.catch((err) => console.log('Warning:', err.message));
    

    所以主要是包装 Promise.all 和使用 Array.map() 实际返回您正在迭代的承诺

    另一件事是不要过度使用积木 {} . 只要返回arrow函数,不管怎样,只要有一个东西在里面。可选择删除 .catch() 只允许抛出这个函数中的错误。调试之后,实际上应该删除该行并允许抛出错误。

        2
  •  0
  •   M14    6 年前

    理想情况下,旅游代码应该有效。问题可能是因为 createSchema() 函数不返回承诺。

         const createSchema = () => {
          Business.findAll({
            raw: true,
          }).then((data) => {
            data.forEach((client,index) => {
    
              postgresDB.createSchema(client.code).then(() => {
                Object.keys(postgresDB.models).forEach((currentItem) => {
                  postgresDB.models[currentItem].schema(client.code).sync();
                   if(index==data.length-1)
                    return true;
                });
                console.log('Postgres schema created');
              }).catch(() => {
              });
            });
          }).catch((err) => {
            console.log('Warning:', err.message);
          });
        };