node.js - 依次执行 node.js 代码
问题描述
client.hydrated().then(function (client) {
client.query({ query: x})
.then(function logData(data) {
console.log('results of query: ', data);
fs.writeFileSync('notesdata.json', JSON.stringify(data))
})
.catch(console.error);`enter code here`
});
var xy=fs.readFileSync('notesdata.json');
这是 AWS sdk 的一部分。从文件中读取数据的最后一行首先执行,然后执行此函数。我知道回调函数但不知道如何在这里实现它,我可以得到一些帮助。谢谢你。
解决方案
承诺被错误地使用。它们应该始终链接起来以提供一致的控制流和错误处理:
client.hydrated().then(function (client) {
return client.query({ query: x})
})
.then(function logData(data) {
console.log('results of query: ', data);
fs.writeFileSync('notesdata.json', JSON.stringify(data))
// no real need to read it from file because it's already available as `data`
var xy=fs.readFileSync('notesdata.json');
})
.catch(console.error);
推荐阅读
- ios - SwiftUI Core Data 保存单个值
- google-chrome - 允许 Tampermonkey(或任何特定的 Chrome 扩展)访问 chrome:方案页面
- r - 仅使用均值和 95% 置信区间估计的点须系数图(不是产生它们的数据)
- javascript - Traversing DOM to remove class using typescript
- javascript - JavaScript for comparing combo date with date picker date
- java - Java Postgresql 如何自动生成主键
- php - Foreach loop issue with multiple arrays
- javascript - Using Jest to test scripts affecting the DOM
- c# - 从 C# 在 Oracle 数据库中存储大值
- r - Group data by year and filter by month in R