编写嵌套的promise javascript es6

时间:2017-10-02 20:43:43

标签: javascript ecmascript-6 promise

有没有人有关于如何编写嵌套承诺的任何指南或提示?

我正在尝试将以下嵌套for loop转换为承诺:

let arr = [1, 2, 3, 4, 5]


for (i = 0; i < arr.length ; i++) {

  for (j = i + 1; j < arr.length ; j++) {

      // call another asynchronous function

  }

}

我考虑过做Promise.all,但内部for循环中的迭代器从j = i + 1开始,因此我不确定如何使用Promise.all处理此问题

提前致谢!

1 个答案:

答案 0 :(得分:1)

将它们推入数组

public boolean insertEmployeeDetails(Employee employee)
    {
        logger.debug("Starting EmployeeDaoImpl.insert()  .....");
        Session session = null;
        Transaction tx = null;
        boolean status = true;
        try {
            session = sessionFactory.openSession();
            tx = session.beginTransaction();
            String hqlInsert = "INSERT INTO Employee (name,definition,"
                    + "( SELECT value_emp_id FROM COMPANY_DATA WHERE testing_id = 1234 AND"
                    + " company_employee_id = 3345))";

            int createdEntities = session.createQuery( hqlInsert )
                    .executeUpdate();

            session.persist(employee);
            tx.commit();
            System.out.println("Checking for hqlInsert");
            System.out.println(hqlInsert);

            System.out.println("Checking for CreatedEntities");
            System.out.println(createdEntities);
        } catch(Exception ex) {
            tx.rollback();
            ex.printStackTrace();
            status = false;
        } finally {
            session.close();
        }
        logger.debug("Completed EmployeeDaoImpl.insert()  .....");
        return status;
    }