【发布时间】:2019-10-21 10:06:41
【问题描述】:
jq.run('.', '/path/to/file.json').then(console.log) 是异步的,所以当我尝试使用它时,我得到了这个:Promise { <pending> } 然后我得到了结果,但为时已晚......那么我该如何解决这个问题?
我试着用await 等待,但我不知道我可以把这个关键字放在哪里。所以这是我的代码:
const jq = require('node-jq')
const filter = '[.root[].A[].AT]'
const jsonPath = './simple.json'
data = jq.run(filter, jsonPath).then((output) => {
console.log(output)
}).catch((err) => {
console.error(err)
})
fs.appendFile('./jqTest.txt', data + "\r\n", function (err) {
if (err) throw err;
console.log("complete!")
});
【问题讨论】:
-
您已经有一个
.catch(),只需添加一个.then()。 -
所以我在 catch(....) 之后添加 .then() ?
-
这就是你对 Promise 所做的事情。
-
我在 catch 之后添加 .then() 但我什么也没得到,这是相同的结果,所以你要求我这样做:
data = jq.run(filter, jsonPath).then((output) => { console.log(output) }).catch((err) => { console.error(err) }).then()如果你要求我这样做它不起作用 -
我不明白你能解释一下吗?
标签: javascript node.js json promise jq