いつもお世話になっております。
Promiseの中にループ処理を入れたいのですが、よくわかっておらずうまくできません。
ご教授をお願いします。
//種類の登録
function setkind(){
var kind = document.getElementById(‘target-date’).value;
var updateList = [];
var searchList = [];
var updateList2 = [];
var limit = 500;
var tmpRecords = tmpRecords || [];
var hdDataList = [];
//Aアプリから更新対象のレコード番号を取得
return kintone.api(kintone.api.url(‘/k/v1/records’, true), ‘GET’, param).then((resp) => {
if(resp.records.length == 0){
swal(‘データがありません’, ‘’, ‘success’);
}else{
for (var i=0; i < resp.records.length; i++) {
var listVal = {
‘id’:resp.records[i][‘レコード番号’].value,
‘record’:{
‘種類’:{value:kind}
}
};
updateList.push(listVal); //Aアプリ更新用
searchList.push(resp.records[i][‘登録番号’].value); //Bアプリ検索用
}
}
}).then((resp2) => {
//Bアプリから更新対象のレコードを取得
if(searchList.length > 0){
var myPromise = Promise.resolve();
for (var i=0; i < searchList.length; i++) {
var param2 = {
app: Bアプリ,
query: ‘登録番号 = "’ + searchList[i] + ‘"’
};
myPromise = myPromise.then(task1.bind(this, param2)); //←★ここがわからない
}
}).then((resp3) => {
//Bアプリへ種類の更新
if(searchList.length > 0){
var param3 = {
app: Bアプリ,
records: updateList2
};
console.log(param3);
return kintone.api(kintone.api.url(‘/k/v1/records’, true), ‘PUT’, param3).then((resp3) => {
console.log(“BアプリUp”);
}, function (resp3) {
alert(“種類登録エラー”);
console.error(‘種類登録エラー’, param3, resp3);
});
}
}).then((resp4) => {
//Aアプリへの更新
var param4 = {
app: Aアプリ,
records: updateList
};
return kintone.api(kintone.api.url(‘/k/v1/records’, true), ‘PUT’, param4).then((resp4) => {
console.log(“AアプリUp”);
}, function (resp1) {
});
});
// ループで実行する処理
function task1(param2){
return new Promise(function(resolve, reject) {
setTimeout(function(){
return kintone.api(kintone.api.url(‘/k/v1/records’, true), ‘GET’, param2).then((resp2) => {
var listVal2 = {
‘id’:resp2.records[0][‘レコード番号’].value,
‘record’:{
‘種類’:{value:kind}
}
};
updateList2.push(listVal2);
}, function (resp2){
});
resolve();
}, 1000);
});
}
}