# for of文における同期処理について

**URL:** <https://community.cybozu.dev/t/topic/6519>\
**Category:** kintone 開発相談\
**Created:** [2020 年 7 月 5 日午前 8:24 UTC](https://community.cybozu.dev/t/topic/6519 "2020-07-05T08:24:35Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Legacy\_Account641](https://avatars.discourse-cdn.com/v4/letter/l/5daacb/32.png) [@Legacy\_Account641](https://community.cybozu.dev/u/Legacy_Account641)\
**Post date:** [2020 年 7 月 5 日午前 8:24 UTC](https://community.cybozu.dev/t/topic/6519/1 "2020-07-05T08:24:35Z")

</div>

いつもお世話になります。

表題の内容を教えてください。

var records = [];

return kintoneUtility.rest.getAllRecordsByQuery(param1).then((allRecords) =\> {  
for (var rec of allRecords[‘records’]) {  
tableValueFetch(rec[‘コード’].value, rec[‘成績表’].value).then((param) =\> {  
records.push(param, appId);  
});  
}  
return records  
}).then((records) =\> { ・・・

上記のコードで、for of文で処理した結果をrecordsとして返したいのですが、for of文の処理が実施されず、recordsが空で返されてしまいます。

具体的な訂正コードを教えて頂ければ、非常に助かります。

よろしくお願い申し上げます。

---

<div class="post-metadata">

**Author:** ![Legacy\_Account1740](https://avatars.discourse-cdn.com/v4/letter/l/ed8c4c/32.png) [@Legacy\_Account1740](https://community.cybozu.dev/u/Legacy_Account1740)\
**Post date:** [2020 年 7 月 5 日午前 8:46 UTC](https://community.cybozu.dev/t/topic/6519/2 "2020-07-05T08:46:25Z")

</div>

Yasuhiro K様

お世話になっております．

tableValueFetch()が非同期処理ということでしょうか？

複数の非同期処理を待機する場合は，Promise.all()を利用するとよいです．  
[https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global\_Objects/Promise/all](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Promise/all)

```
var records = [];
return kintoneUtility.rest.getAllRecordsByQuery(param1).then((allRecords) => {
  return Promise.all(allRecords['records'].map((rec) => {
    return tableValueFetch(rec['コード'].value, rec['成績表'].value);
  }));
}).then((params) => {
  params.forEach((param) => {
    records.push(param, appId);
  });
});

```

---

<div class="post-metadata">

**Author:** ![Legacy\_Account641](https://avatars.discourse-cdn.com/v4/letter/l/5daacb/32.png) [@Legacy\_Account641](https://community.cybozu.dev/u/Legacy_Account641)\
**Post date:** [2020 年 7 月 5 日午前 10:02 UTC](https://community.cybozu.dev/t/topic/6519/3 "2020-07-05T10:02:11Z")

</div>

江田様

&nbsp;

いつもお世話になります。

江田様の書かれた内容をいつも確認させて頂き、勉強させていただいております。

ありがとうございます。

お陰さまで非同期処理の問題は解消されました。

ただ、まだ問題がございます。

違うアプリからレコードをGETして、参照元のアプリのテーブルにそのデータを追加したいと考えています。

下記のコードを実行するとBad Requestが返ってきます。

お手数ですが、ご確認頂き、間違いをご指摘頂けますでしょうか?

よろしくお願い申し上げます。

&nbsp;

//実行タイミングはレコード詳細画面を開いた時  
kintone.events.on(“app.record.detail.show”, (event) =\> {

var appId = 100

var param1 = {  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;app: appId,  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;field: [‘コード’, ‘成績表’]  
&nbsp; &nbsp; &nbsp; &nbsp; };

&nbsp; &nbsp; &nbsp; &nbsp; var records = [];  
&nbsp; &nbsp; &nbsp; &nbsp; return kintoneUtility.rest.getAllRecordsByQuery(param1).then((allRecords) =\> {  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return Promise.all(allRecords[‘records’].map((rec) =\> {  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return tableValueFetch(rec[‘YLコード’].value, rec[‘成績表’].value);  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}));  
&nbsp; &nbsp; &nbsp; &nbsp; }).then((params) =\> {  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;params.forEach((param) =\> {  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; records.push(param, appId);  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;});  
&nbsp; &nbsp; &nbsp; &nbsp; }).then(() =\> {

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var param2 = {  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; app: appId,  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; records: records  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return kintoneUtility.rest.putAllRecords(param2).then((resp) =\> {  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;console.log(JSON.stringify(resp, null, ’ '));  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }).catch((error) =\> {  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(error.message);  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });  
&nbsp; &nbsp; &nbsp; &nbsp;});  
&nbsp; &nbsp; }  
});

//コードから成績表アプリの該当レコードを取得  
function tableValueFetch(aCode, tableRecords) {  
&nbsp; &nbsp; &nbsp;var refAppId = 459;  
&nbsp; &nbsp; &nbsp;var params = {  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;app: refAppId,  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;query: ‘コード="’ + ylCode + ‘"’,  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//レスポンスに含めるフィールド指定  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fields: [“$id”, “対象年月”, “組織名”, “番号”, “顧客軒数”,  
&nbsp; &nbsp; &nbsp; “本数1”, “本数2”, “合計金額”]  
&nbsp; &nbsp; &nbsp; };

&nbsp; &nbsp; &nbsp; //引用元のアプリからレコードGET  
&nbsp; &nbsp; &nbsp; return kintoneUtility.rest.getAllRecordsByQuery(params).then((resp) =\> {  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//変数に「r」にレスポンスで返ってきたものを代入、  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//レスポンスがなくなるまで繰り返す  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for (var rec of resp[‘records’]) {  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var tableRecord = {  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; value: {  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; “対象年月”: {  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; value: “\<div\>\<a href="/k/” + refAppId + “/show#record=”  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;+ rec[‘$id’].value + “"\>”  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;+ rec[‘対象年月’].value  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;+ “\</a\>\</div\>”  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;“成績表\_組織名”: {  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; value: rec[‘組織名’].value  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;},  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; “成績表\_番号”: {  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; value: rec[‘地区番号’].value  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;},  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; “顧客軒数”: {  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; value: rec[‘顧客軒数’].value  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; “本数1”: {  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; value: rec[‘本数1’].value  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"本数"2: {  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; value: rec[‘本数2’].value  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;},  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;“売上金額”: {  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; value: rec[‘合計金額’].value  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;};  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;tableRecords.push(tableRecord);  
&nbsp; &nbsp; &nbsp;}

&nbsp; &nbsp; &nbsp;var record = {  
&nbsp; &nbsp; &nbsp; &nbsp; ‘成績表’: {  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; value: tableRecords  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}  
&nbsp; &nbsp; };

&nbsp; &nbsp; var key = {  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;field: ‘コード’,  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;value: aCode  
&nbsp; &nbsp; };

&nbsp; &nbsp; var records = {  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;updateKey: key,  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;record: record  
&nbsp; &nbsp; }

&nbsp; &nbsp; &nbsp;return records;  
&nbsp; &nbsp;});

&nbsp;}

---

<div class="post-metadata">

**Author:** ![Legacy\_Account641](https://avatars.discourse-cdn.com/v4/letter/l/5daacb/32.png) [@Legacy\_Account641](https://community.cybozu.dev/u/Legacy_Account641)\
**Post date:** [2020 年 7 月 5 日午前 10:06 UTC](https://community.cybozu.dev/t/topic/6519/4 "2020-07-05T10:06:29Z")

</div>

江田さま

汚いコードで申し訳ございません。

コメントは間違えているかもしれませんので、無視して頂ければと思います。

下記は間違えています。

×

&nbsp; &nbsp; &nbsp; “成績表\_番号”: {  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; value: rec[‘地区番号’].value  
&nbsp; &nbsp; &nbsp; &nbsp;},

○

&nbsp; &nbsp; &nbsp; “成績表\_番号”: {  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; value: rec[‘地区番号’].value  
&nbsp; &nbsp; &nbsp; &nbsp;},

フィールドコードは企業情報もございますので、訂正しましたので、訂正ミスがありかもしれません。

重ね重ね申し訳ございません。

&nbsp;

---

<div class="post-metadata">

**Author:** ![Legacy\_Account641](https://avatars.discourse-cdn.com/v4/letter/l/5daacb/32.png) [@Legacy\_Account641](https://community.cybozu.dev/u/Legacy_Account641)\
**Post date:** [2020 年 7 月 5 日午前 10:08 UTC](https://community.cybozu.dev/t/topic/6519/5 "2020-07-05T10:08:27Z")

</div>

×&nbsp; &nbsp; &nbsp;

var params = {  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;app: refAppId,  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;query: ‘コード="’ + \_ **ylCode** \_ + ‘"’,  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//レスポンスに含めるフィールド指定  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fields: [“$id”, “対象年月”, “組織名”, “番号”, “顧客軒数”,  
&nbsp; &nbsp; &nbsp; “本数1”, “本数2”, “合計金額”]  
&nbsp; &nbsp; &nbsp; };

○

&nbsp; &nbsp; &nbsp;var params = {  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;app: refAppId,  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;query: ‘コード="’ + **\_a\_Code** + ‘"’,  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//レスポンスに含めるフィールド指定  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fields: [“$id”, “対象年月”, “組織名”, “番号”, “顧客軒数”,  
&nbsp; &nbsp; &nbsp; “本数1”, “本数2”, “合計金額”]  
&nbsp; &nbsp; &nbsp; };

&nbsp;

---

<div class="post-metadata">

**Author:** ![Legacy\_Account641](https://avatars.discourse-cdn.com/v4/letter/l/5daacb/32.png) [@Legacy\_Account641](https://community.cybozu.dev/u/Legacy_Account641)\
**Post date:** [2020 年 7 月 5 日午後 12:05 UTC](https://community.cybozu.dev/t/topic/6519/6 "2020-07-05T12:05:32Z")

</div>

お世話になります。

ありがとうございました。

自己解決いたしました。

&nbsp;

×

&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; records.push(param, appId);

○

&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; records.push(param);

&nbsp;
