How to wrap async function calls into a sync function in Node.js or Javascript?
问题 Suppose you maintain a library that exposes a function getData . Your users call it to get actual data: var output = getData(); Under the hood data is saved in a file so you implemented getData using Node.js built-in fs.readFileSync . It\'s obvious both getData and fs.readFileSync are sync functions. One day you were told to switch the underlying data source to a repo such as MongoDB which can only be accessed asynchronously. You were also told to avoid pissing off your users, getData API