js外部函数返回内部函数返回值?
发布网友
发布时间:2022-05-06 05:41
我来回答
共1个回答
热心网友
时间:2022-06-29 15:44
async function getIdByPhone(phone) {
openConnection();
let params = [phone];
let query = 'select id from user where phone = ?';
let userId = await new Promise((resolve, reject) => {
connection.query(query, params, (err, result) => {
if (err) {
console.error('[GET ERROR] - ', err.message);
return resolve();
}
if (result.length != 0) {
console.log('get user id: ' + result[0].id);
resolve(result[0].id); // 查找得到用户id
} else {
console.error('not found with phone : ' + phone);
resolve();
}
})
});
closeConnection();
return userId; // 返回用户id
}
getIdByPhone().then(res => console.log(res)) //res是结果