I had my custom module with following code:
module.exports.PrintNearestStore = async function PrintNearestStore(session, lat, lon) {
...
}
It worked fine if call the function outside my module, however if I called inside I got error while running:
(node:24372) UnhandledPromiseRejectionWarning: Unhandled promise
rejection (rejection id: 1): ReferenceError: PrintNearestStore is not
defined
When I changed syntax to:
module.exports.PrintNearestStore = PrintNearestStore;
var PrintNearestStore = async function(session, lat, lon) {
}
It started to work fine inside module, but fails outside the module - I got error:
(node:32422) UnhandledPromiseRejectionWarning: Unhandled promise
rejection (rejection id: 1): TypeError: mymodule.PrintNearestStore is
not a function
So I've changed code to:
module.exports.PrintNearestStore = async function(session, lat, lon) {
await PrintNearestStore(session, lat, lon);
}
var PrintNearestStore = async function(session, lat, lon) {
...
}
And now it works in all cases: inside and outside. However want to understand semantics and if there is more beautiful and shorter way to write it? How to correctly define and use async function both: inside and outside (exports) module?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…