Perhaps the problem is that you are completing the request process before all calls are completed, you can try Promise.all ():
account.instance('instance').class('competition').dataobject().list(filter) .then(function(compRes) { var competitionID = compRes.objects[0].id, promises=[]; for (var i = 0; i < teamArray.length; i++) { newObject = { "name": teamArray[i].name, "nameshort": teamArray[i].code, "logo": teamArray[i].crestUrl, "competition": competitionID }; promises.push(account.instance('instance').class('teams').dataobject().add(newObject).then(function(res) { console.log(res); }).catch(function(err) { console.log("Error eq: " + err); }) ); } return Promise.all(promises); }).catch(function(err) { console.log(err); });
if too many concurrent calls at a time is a problem, then chaining one call after another:
account.instance('instance').class('competition').dataobject().list(filter) .then(function(compRes) { var competitionID = compRes.objects[0].id, promise = Promise.resolve(); function chainToPromise(promise, teamObj, waitTime){ waitTime = waitTime || 500; return promise.then(function(){ return new Promise(function(resolve, reject){ setTimeout(resolve, waitTime); }); }).then(function(){ return account.instance('instance').class('teams').dataobject().add(teamObj); }).then(function(res) { console.log(res); }).catch(function(err) { console.log("Error eq: " + err); }); } for (var i = 0; i < teamArray.length; i++) { newObject = { "name": teamArray[i].name, "nameshort": teamArray[i].code, "logo": teamArray[i].crestUrl, "competition": competitionID }; promise = chainToPromise(promise, newObject); } return promise; }).catch(function(err) { console.log(err); });
source share