首页 > 解决方案 > 依次执行 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 的一部分。从文件中读取数据的最后一行首先执行,然后执行此函数。我知道回调函数但不知道如何在这里实现它,我可以得到一些帮助。谢谢你。

标签: node.jsamazon-web-servicesasynchronous

解决方案


承诺被错误地使用。它们应该始终链接起来以提供一致的控制流和错误处理:

   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);

推荐阅读