回答編集履歴
2
mysqlを使って自前のPromiseで処理するとすると...
test
CHANGED
@@ -93,3 +93,73 @@
|
|
93
93
|
})();
|
94
94
|
|
95
95
|
```
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
最後に上のサンプルをmysqlを使って自前のPromiseで処理するとすると...
|
100
|
+
|
101
|
+
```javascript
|
102
|
+
|
103
|
+
const mysql = require('mysql');
|
104
|
+
|
105
|
+
|
106
|
+
|
107
|
+
async function mysql_createConnection(option) {
|
108
|
+
|
109
|
+
return Promise.resolve(mysql.createConnection(option));
|
110
|
+
|
111
|
+
}
|
112
|
+
|
113
|
+
|
114
|
+
|
115
|
+
async function connection_query(connection, query) {
|
116
|
+
|
117
|
+
return new Promise((resolve, reject) => {
|
118
|
+
|
119
|
+
connection.query(query, (error, results, fields) => {
|
120
|
+
|
121
|
+
if (error) {
|
122
|
+
|
123
|
+
reject(error);
|
124
|
+
|
125
|
+
} else {
|
126
|
+
|
127
|
+
resolve(results);
|
128
|
+
|
129
|
+
}
|
130
|
+
|
131
|
+
});
|
132
|
+
|
133
|
+
})
|
134
|
+
|
135
|
+
}
|
136
|
+
|
137
|
+
|
138
|
+
|
139
|
+
(async () => {
|
140
|
+
|
141
|
+
const connection = await mysql_createConnection({
|
142
|
+
|
143
|
+
host: 'localhost',
|
144
|
+
|
145
|
+
user: 'homestead',
|
146
|
+
|
147
|
+
database: 'homestead',
|
148
|
+
|
149
|
+
password: 'secret',
|
150
|
+
|
151
|
+
port: '13306'
|
152
|
+
|
153
|
+
});
|
154
|
+
|
155
|
+
|
156
|
+
|
157
|
+
const rows = await connection_query(connection, `SELECT * FROM item;`);
|
158
|
+
|
159
|
+
console.log(rows);
|
160
|
+
|
161
|
+
connection.end();
|
162
|
+
|
163
|
+
})();
|
164
|
+
|
165
|
+
```
|
1
一応promise-mysqlのサンプルも書いときました。
test
CHANGED
@@ -55,3 +55,41 @@
|
|
55
55
|
});
|
56
56
|
|
57
57
|
```
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
一応promise-mysqlのサンプルも書いときました。
|
62
|
+
|
63
|
+
```javascript
|
64
|
+
|
65
|
+
const mysql = require('promise-mysql');
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
(async () => {
|
70
|
+
|
71
|
+
const connection = await mysql.createConnection({
|
72
|
+
|
73
|
+
host: 'localhost',
|
74
|
+
|
75
|
+
user: 'homestead',
|
76
|
+
|
77
|
+
database: 'homestead',
|
78
|
+
|
79
|
+
password: 'secret',
|
80
|
+
|
81
|
+
port: '13306'
|
82
|
+
|
83
|
+
});
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
const rows = await connection.query(`SELECT * FROM item;`);
|
88
|
+
|
89
|
+
console.log(rows);
|
90
|
+
|
91
|
+
connection.end();
|
92
|
+
|
93
|
+
})();
|
94
|
+
|
95
|
+
```
|