如何打破从内部承诺函数触发的循环?

时间:2018-04-19 07:58:58

标签: javascript node.js promise sequelize.js

如何打破续集的承诺.then({})。我想打破退出到当时的承诺并打破循环。当条件触发时。代码与以下相同。不是我的确切代码。

int i = 0;
   while(i<result.length){

      db.Model.findAll({
      where:{
         x : X,
         w : W,
         z : z
      }).then(result => {
        if(result[i].x == null || result[i].x == undefined){
             // update and break here!
             update(): // something like that
             // how i break the outer loop.
        }
        if(result[i].w > 0){
             // how i break the outer loop.
        }
        if(result[i].z < 100){
             // how i break the outer loop.
        }
   })
 i++;
}

1 个答案:

答案 0 :(得分:0)

您需要的只是async / await并按照以下方式实施:

async function your_function(){
    // init your result varibale
    for(let i=0 ; i < result.length ; i++) {
        let mresult = await db.Model.findAll({
                                            where:{
                                                x : X,
                                                w : W,
                                                z : z
                                            })
        if(mresult[i].x == null || mresult[i].x == undefined){
             update(): // something like that
             break; // <------------- Break it like this
        }
        if(mresult[i].w > 0){
             break; // <------------- Break it like this
        }
        if(mresult[i].z < 100){
             break; // <------------- Break it like this
        }
    }    
}
相关问题