質問編集履歴
2
追記しました
test
CHANGED
File without changes
|
test
CHANGED
@@ -31,3 +31,75 @@
|
|
31
31
|
console.log(JSON.stringify(temp)); // 実行結果: undefined
|
32
32
|
|
33
33
|
```
|
34
|
+
|
35
|
+
**■追記**
|
36
|
+
|
37
|
+
**以下の方法で解決できましたが、いまいち動作がわかりません。**
|
38
|
+
|
39
|
+
以下の理解で正しいでしょうか?
|
40
|
+
|
41
|
+
① 処理待ちしたい関数は Promise でリターンする。
|
42
|
+
|
43
|
+
② 処理待ちしたい関数の受けて側は await で Promise が resolve になるまで待つ。
|
44
|
+
|
45
|
+
③ awaitを持つ関数は宣言の前に await を宣言する。
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
以下今回のコードと実行結果になります。
|
50
|
+
|
51
|
+
いまいち挙動の理解ができていないので コードが不安であります。他に記述する方法や間違っている点があれば教えていただきたいです。よろしくお願いいたします。
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
**■コード**
|
56
|
+
|
57
|
+
```Javascript
|
58
|
+
|
59
|
+
function test() {
|
60
|
+
|
61
|
+
return new Promise(function(resolve, reject) {
|
62
|
+
|
63
|
+
evdDB.evdNoteStore
|
64
|
+
|
65
|
+
.toArray()
|
66
|
+
|
67
|
+
.then(function(notes) {
|
68
|
+
|
69
|
+
console.log(notes);
|
70
|
+
|
71
|
+
resolve(notes);
|
72
|
+
|
73
|
+
});
|
74
|
+
|
75
|
+
});
|
76
|
+
|
77
|
+
}
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
async function call() {
|
82
|
+
|
83
|
+
console.log("inner call start");
|
84
|
+
|
85
|
+
var result = await test();
|
86
|
+
|
87
|
+
console.log(JSON.stringify(result));
|
88
|
+
|
89
|
+
console.log("inner call end");
|
90
|
+
|
91
|
+
}
|
92
|
+
|
93
|
+
|
94
|
+
|
95
|
+
console.log("outer call start");
|
96
|
+
|
97
|
+
call()
|
98
|
+
|
99
|
+
console.log("outer call end");
|
100
|
+
|
101
|
+
```
|
102
|
+
|
103
|
+
**■実行結果**
|
104
|
+
|
105
|
+
![イメージ説明](7abd0929f50c5d439fa7dddd22950a5d.png)
|
1
タイトルを変更しました。
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
非同期処理を待ち、変数を代入したい(IndexDB, Dexie.js)
|
1
|
+
非同期処理を待ち、そこで得た値を変数を代入したい(IndexDB, Dexie.js)
|
test
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
非同期処理を待ち、変数を代入したいです。
|
1
|
+
**非同期処理を待ち、変数を代入したいです。**
|
2
2
|
|
3
3
|
仮のテストコードを書いて実行しているのですが、非同期処理のところで詰まっています。
|
4
4
|
|