File tree Expand file tree Collapse file tree 2 files changed +25
-0
lines changed
lessons/02-promises/lecture Expand file tree Collapse file tree 2 files changed +25
-0
lines changed Original file line number Diff line number Diff line change @@ -116,4 +116,11 @@ getPersonVehicles(1)
116116 .then ((allVehicles ) => {
117117 console .log (allVehicles )
118118 })
119+
120+ // ----------------
121+ // 7. Show database connection and wrapping callbacks in promises
122+
123+ import { query } from ' ./db'
124+
125+ query (' SELECT * FROM user' ).then ((results ) => {})
119126```
Original file line number Diff line number Diff line change 1+ import path from 'path'
2+ import sqlite3 from 'sqlite3'
3+
4+ const db = new sqlite3 . Database ( path . resolve ( process . cwd ( ) , 'database.db' ) )
5+
6+ export function query ( sql : string ) {
7+ return new Promise ( ( resolve , reject ) => {
8+ db . all ( sql , ( err , rows ) => {
9+ if ( err ) {
10+ reject ( err . message )
11+ } else {
12+ resolve ( rows || [ ] )
13+ }
14+ } )
15+ } )
16+ }
17+
18+ // query('SELECT * FROM user').then(results => {})
You can’t perform that action at this time.
0 commit comments