I'm wondering if there is a better way to do an asynchronous loop in JavaScript? I've been using the following recursive method but I feel like there is probably a cleaner way. Any links / advice would be much appreciated. Thanks
var titles = ['Test 1', 'Test 2', 'Test 3'];
var i = 0;
addColumns();
function addColumns () {
if (i < titles.length) {
var data = {
'__metadata': { 'type': 'SP.FieldText' },
'FieldTypeKind': 3,
'Title': titles[i],
'MaxLength': '22'
};
postToSP.createColumns(baseURL, listName, data)
.then(function () {
i++;
addColumns();
})
.catch(function(e){
console.log('Error: ' + e);
})
} else {
return;
};
};