ES6 import/export 合并对象与异步等待
ES6 import/export merge object with async await
我有两个名为 config.js
和 config.json
的文件,我想将它们合并为一个对象并导出:
config.json
{
"c": 3
}
config.js
import fs from "fs";
import fse from "fs-extra";
import watch from "node-watch";
const config = {
a: 1
b: 2
}
//check does file exists
if (fs.existsSync('./src/config.json')) {
//event emitter watch for changes in json
const watcher = watch('./src/config.json', { recursive: true });
//if json ready, then validate and merge
watcher.on('ready', async function() {
const importConfig = await fse.readJson('./src/config.json');
//merge both default config from current file and json
Object.assign(config, importConfig)
//small validate function which checks that `json` can be merged
const valid = await validationConfig(config)
if (valid) {
logger.info('Settings has been valid and imported successfully.');
} else {
process.exit(1);
}
});
}
//console.log(settings) a & b exists, but c does not
export { config };
但不知何故,即使使用 fs.existsSync
export 关键字也不会等待 if
块内的代码结束,而是先于结束。如何解决这个问题?
watcher.on('ready', async function() {
是一个回调。回调下方的同步代码 - 即 export { config }
- 将在回调完成前 运行。
改为导出一个 Promise,它解析为加载配置的值:
const config = { a: 1, b: 2 };
export const configProm = !fs.existsSync('./src/config.json')
? Promise.resolve(config)
: new Promise((resolve, reject) => { // <----------------------------
const watcher = watch('./src/config.json', { recursive: true });
watcher.on('ready', async function () {
const importConfig = await fse.readJson('./src/config.json');
Object.assign(config, importConfig)
const valid = await validationConfig(config)
if (valid) {
logger.info('Settings has been valid and imported successfully.');
resolve(config); // <----------------------------
} else {
process.exit(1);
reject(); // ??
}
});
});
我有两个名为 config.js
和 config.json
的文件,我想将它们合并为一个对象并导出:
config.json
{
"c": 3
}
config.js
import fs from "fs";
import fse from "fs-extra";
import watch from "node-watch";
const config = {
a: 1
b: 2
}
//check does file exists
if (fs.existsSync('./src/config.json')) {
//event emitter watch for changes in json
const watcher = watch('./src/config.json', { recursive: true });
//if json ready, then validate and merge
watcher.on('ready', async function() {
const importConfig = await fse.readJson('./src/config.json');
//merge both default config from current file and json
Object.assign(config, importConfig)
//small validate function which checks that `json` can be merged
const valid = await validationConfig(config)
if (valid) {
logger.info('Settings has been valid and imported successfully.');
} else {
process.exit(1);
}
});
}
//console.log(settings) a & b exists, but c does not
export { config };
但不知何故,即使使用 fs.existsSync
export 关键字也不会等待 if
块内的代码结束,而是先于结束。如何解决这个问题?
watcher.on('ready', async function() {
是一个回调。回调下方的同步代码 - 即 export { config }
- 将在回调完成前 运行。
改为导出一个 Promise,它解析为加载配置的值:
const config = { a: 1, b: 2 };
export const configProm = !fs.existsSync('./src/config.json')
? Promise.resolve(config)
: new Promise((resolve, reject) => { // <----------------------------
const watcher = watch('./src/config.json', { recursive: true });
watcher.on('ready', async function () {
const importConfig = await fse.readJson('./src/config.json');
Object.assign(config, importConfig)
const valid = await validationConfig(config)
if (valid) {
logger.info('Settings has been valid and imported successfully.');
resolve(config); // <----------------------------
} else {
process.exit(1);
reject(); // ??
}
});
});