Tuesday 23 May 2017

I get empty array from promise

I'm trying to use promises, but I get an empty array out:

    var getProjectTestCases = function (testproject_ids){
      out = [];
      return new Promise(function (resolve, reject){
        testproject_ids.forEach(testproject_id => {
          testlink.getProjectTestCases({testprojectid: testproject_id.id}, testcases => {
            if(!Array.isArray(testcases)){
              if(testcases.string)
                out.push({id: parseInt(testcases.string)});
            }else{
              for(testcase of testcases){
                out.push({id: parseInt(testcase.string)});
              }
            }
          });
        });
//return Promise.all(out) or resolve(out)
        return Promise.all(out);
      });
    };

//empty array
getProjectTestCases([{id: 1},{id: 49}]).then(a => console.log(a));

That is my second solution, cause first I wroteresolve(out) instead of return Promise.all(out)



via MrQwenty

No comments:

Post a Comment