简体   繁体   English

node.js中的属性

[英]Properties in node.js

Imagine this scenario: I want to program an application in nodejs which has to be easily configurable. 想象一下这种情况:我想在nodejs中编写一个必须易于配置的应用程序。 To do this I choose create a properties file which will be read by a module of nodejs called properties. 为此,我选择创建一个属性文件,该文件将由称为属性的nodejs模块读取。 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. 我可以将此函数放在一个promise中,但这不能解决问题,只能向前发展。 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. 示例:如果将日志记录配置保存在属性文件中,则可以运行promise并将代码将日志文件写入“ then”中,这意味着,主功能可以在写入日志文件之前完成。 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? 一种解决方案是将所有代码放入then函数中,但是...如果我的函数必须返回值,会发生什么? 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])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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