简体   繁体   中英

Properties in node.js

Imagine this scenario: I want to program an application in nodejs which has to be easily configurable. To do this I choose create a properties file which will be read by a module of nodejs called properties. The problem is that the function to read the properties file is asynchronous, I mean, I know when it start but not when it finish. I can put this function inside a promise but this not solve the problem, it only moves forward. Example: If I save the logging configuration in the properties file, I can run a promise and put the code to write the log file inside the "then", which means that, the main function could finish before the log file is written. At the end, the log file could be written or not, it depends how the promise finished. A solution is to put all the code inside of the then function but... What happens if my function has to return a value? I can not put this inside a promise.

I would like to know If any of you have had this problem and how have solved

You can read files synchronously if you need to;

var fs = require('fs');

var contents = fs.readFileSync(filename, [options])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM