等待不是暂停执行
Await is not pausing execution
我不明白为什么执行会继续,并且 await 不会暂停执行,直到被调用的函数 returns。
在 node.js 应用程序中
Contacts.js
async function routes (fastify, options) {
fastify.get('/contact', async (req, reply) => {
let lookup = require('../helpers/validate_school');
let school_info = await lookup.validate(req.hostname.split('.')[0]);
console.log('here: ', school_info);
let school = school_info.school;
...
reply.view('application/application.html', school);
});
};
school.lookup.js
async function validate(hostname){
const con = require('../../config/db').getDb();
let ret = {};
console.log('lets check for school info');
await con.query('SELECT * FROM schools where identifier=? LIMIT ?', [hostname, 1], function(err, res, fields){
if (err) throw err;
if (res.length > 0){
ret.school = JSON.stringify(res[0]);
...
console.log('found: ', ret);
return ret;
} else {
console.log('not found: ', ret);
return ret;
}
});
};
module.exports = {validate: validate};
日志
lets check for school info
here: undefined
found: {
school: '{"id":2,"name":"Second School","school_dbid":"2","primary_color":"purple","secondary_color":"lavender","tertiary_color":"green","quaternary_color":"blue","identifier":"school2","mascot_id":1,"created_at":"2019-11-20T05:22:16.864Z","updated_at":"2019-11-21T17:59:11.956Z"}',
...
}
如何确保 lookup.validate returns 在继续代码块之前?
await
的全部要点是让您不必使用回调。相反,它只会 return 向您提供数据或在拒绝时抛出错误。您需要选择一个,要么只使用回调,要么只使用 async/await.
话虽这么说,async/await 只适用于承诺。但是 mysql
库不使用 promises。因此,如果您使用的是 mysql
而不是 mysql2
.
,那么在这种情况下您甚至不能使用 async/await
此外,回调不会 return 任何东西。 Return 语句在异步场景中不起作用。
你有两个选择。处理回调的异步性,直接使用value:
con.query('SELECT * FROM schools where identifier=? LIMIT ?', [hostname, 1], function(err, res, fields){
// The result cannot leave this callback.
// Anything you need to do with the result must be done here.
});
或者,如果您正在使用 mysql2
,您可以使用这样的承诺:
const data = await con.promise().query('your query');
我不明白为什么执行会继续,并且 await 不会暂停执行,直到被调用的函数 returns。
在 node.js 应用程序中
Contacts.js
async function routes (fastify, options) {
fastify.get('/contact', async (req, reply) => {
let lookup = require('../helpers/validate_school');
let school_info = await lookup.validate(req.hostname.split('.')[0]);
console.log('here: ', school_info);
let school = school_info.school;
...
reply.view('application/application.html', school);
});
};
school.lookup.js
async function validate(hostname){
const con = require('../../config/db').getDb();
let ret = {};
console.log('lets check for school info');
await con.query('SELECT * FROM schools where identifier=? LIMIT ?', [hostname, 1], function(err, res, fields){
if (err) throw err;
if (res.length > 0){
ret.school = JSON.stringify(res[0]);
...
console.log('found: ', ret);
return ret;
} else {
console.log('not found: ', ret);
return ret;
}
});
};
module.exports = {validate: validate};
日志
lets check for school info
here: undefined
found: {
school: '{"id":2,"name":"Second School","school_dbid":"2","primary_color":"purple","secondary_color":"lavender","tertiary_color":"green","quaternary_color":"blue","identifier":"school2","mascot_id":1,"created_at":"2019-11-20T05:22:16.864Z","updated_at":"2019-11-21T17:59:11.956Z"}',
...
}
如何确保 lookup.validate returns 在继续代码块之前?
await
的全部要点是让您不必使用回调。相反,它只会 return 向您提供数据或在拒绝时抛出错误。您需要选择一个,要么只使用回调,要么只使用 async/await.
话虽这么说,async/await 只适用于承诺。但是 mysql
库不使用 promises。因此,如果您使用的是 mysql
而不是 mysql2
.
此外,回调不会 return 任何东西。 Return 语句在异步场景中不起作用。
你有两个选择。处理回调的异步性,直接使用value:
con.query('SELECT * FROM schools where identifier=? LIMIT ?', [hostname, 1], function(err, res, fields){
// The result cannot leave this callback.
// Anything you need to do with the result must be done here.
});
或者,如果您正在使用 mysql2
,您可以使用这样的承诺:
const data = await con.promise().query('your query');