質問編集履歴
2
修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -161,3 +161,81 @@
|
|
161
161
|
!18935
|
162
162
|
|
163
163
|
```
|
164
|
+
|
165
|
+
|
166
|
+
|
167
|
+
### 完成系(※参考)
|
168
|
+
|
169
|
+
(確実に冗長すぎる書き方なので、修正は必要な認識。ただ、やりたいことは実現できたので、一応記載した次第。)
|
170
|
+
|
171
|
+
```javascript
|
172
|
+
|
173
|
+
var tempNum;
|
174
|
+
|
175
|
+
function sampleResolve(testMethod) {
|
176
|
+
|
177
|
+
return new Promise(resolve => {
|
178
|
+
|
179
|
+
setTimeout(() => {
|
180
|
+
|
181
|
+
connection.query('SELECT count(*) as column1 FROM table', (err,rows) => {
|
182
|
+
|
183
|
+
if(err) throw err;
|
184
|
+
|
185
|
+
tempNum = rows[0].column1;
|
186
|
+
|
187
|
+
console.log("!"+tempNum);
|
188
|
+
|
189
|
+
resolve(testMethod(tempNum));
|
190
|
+
|
191
|
+
})
|
192
|
+
|
193
|
+
connection.end();
|
194
|
+
|
195
|
+
}, 4000);
|
196
|
+
|
197
|
+
})
|
198
|
+
|
199
|
+
}
|
200
|
+
|
201
|
+
|
202
|
+
|
203
|
+
function testMethod(tempNum){
|
204
|
+
|
205
|
+
console.log("call back");
|
206
|
+
|
207
|
+
return tempNum;
|
208
|
+
|
209
|
+
}
|
210
|
+
|
211
|
+
|
212
|
+
|
213
|
+
async function sample() {
|
214
|
+
|
215
|
+
const result = await sampleResolve(testMethod);
|
216
|
+
|
217
|
+
return result;
|
218
|
+
|
219
|
+
}
|
220
|
+
|
221
|
+
|
222
|
+
|
223
|
+
sample().then(result => {
|
224
|
+
|
225
|
+
console.log("!!"+result);
|
226
|
+
|
227
|
+
});
|
228
|
+
|
229
|
+
```
|
230
|
+
|
231
|
+
### 完成系のコンソール出力(※参考)
|
232
|
+
|
233
|
+
```
|
234
|
+
|
235
|
+
!18935
|
236
|
+
|
237
|
+
call back
|
238
|
+
|
239
|
+
!!18935
|
240
|
+
|
241
|
+
```
|
1
追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -95,3 +95,69 @@
|
|
95
95
|
|
96
96
|
|
97
97
|
何卒よろしくお願いいたします。
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
### [追記]修正後コード(期待した結果は未得)
|
102
|
+
|
103
|
+
```javascript
|
104
|
+
|
105
|
+
function sampleResolve() {
|
106
|
+
|
107
|
+
return new Promise(resolve => {
|
108
|
+
|
109
|
+
setTimeout(() => {
|
110
|
+
|
111
|
+
resolve(
|
112
|
+
|
113
|
+
connection.query('SELECT count(*) as column1 FROM table', (err,rows) => {
|
114
|
+
|
115
|
+
if(err) throw err;
|
116
|
+
|
117
|
+
tempNum = rows[0].column1;
|
118
|
+
|
119
|
+
console.log("!"+tempNum);
|
120
|
+
|
121
|
+
return tempNum;
|
122
|
+
|
123
|
+
})
|
124
|
+
|
125
|
+
);
|
126
|
+
|
127
|
+
connection.end();
|
128
|
+
|
129
|
+
}, 4000);
|
130
|
+
|
131
|
+
})
|
132
|
+
|
133
|
+
}
|
134
|
+
|
135
|
+
|
136
|
+
|
137
|
+
async function sample() {
|
138
|
+
|
139
|
+
const result = await sampleResolve();
|
140
|
+
|
141
|
+
return result;
|
142
|
+
|
143
|
+
}
|
144
|
+
|
145
|
+
|
146
|
+
|
147
|
+
sample().then(result => {
|
148
|
+
|
149
|
+
console.log("!!"+result);
|
150
|
+
|
151
|
+
});
|
152
|
+
|
153
|
+
```
|
154
|
+
|
155
|
+
### [追記]修正後コードのコンソール
|
156
|
+
|
157
|
+
```
|
158
|
+
|
159
|
+
!![object Object]
|
160
|
+
|
161
|
+
!18935
|
162
|
+
|
163
|
+
```
|