How does Node.JS get the result of asynchronous processing and return to the outermost layer?

for example,
I want to read a json file, convert the content to string, and then use JSON.paras to convert it into a JSON object.
I"m confused right now. Please give me some guidance.

//
let result = require("./func").read2JSON("db/list.json");
console.log(result);
The

result shows undefined.
, so how exactly do you return the result to the outermost layer? What exactly is this principle?

Mar.20,2021

use async/await, to finish asynchronous execution and then return


gives you several ways to write:

callback form:

require('./func').read2JSON('db/list.json').then(result => {
    console.log(result);
});
Menu