diff --git "a/\346\235\250\350\214\227\345\215\232/yang/\346\225\260\346\215\256\345\272\223.js" "b/\346\235\250\350\214\227\345\215\232/yang/\346\225\260\346\215\256\345\272\223.js" new file mode 100644 index 0000000000000000000000000000000000000000..fbc67166caa7c21f5be50fed87a6967bb375d096 --- /dev/null +++ "b/\346\235\250\350\214\227\345\215\232/yang/\346\225\260\346\215\256\345\272\223.js" @@ -0,0 +1,28 @@ +//作业:第一个能不能使用promise的异步函数,封装下数据库的查询 +let mysql =require('mysql'); + +let conn = mysql.createConnection({ + host:"localhost", + user:"root", + password:"root", + database:"123" +}); +conn.connect(); + + +function usePromise(sql){ + return new Promise((resolve,reject)=>{ + conn.query(sql,(err,data)=>{ + if(err!=null)reject(err);//有错误调用 + resolve(data);//无错误调用 + conn.end(); + }) + }) +} + +async function doAsync(sql){ + let a = await usePromise(sql); + console.log(a); +} + +doAsync("select * from stu where stu_name = '张三'"); \ No newline at end of file