我正在使用 nodegit 基于 git 提交创建自定义 CHANGELOG 生成器(我不太喜欢现有的类似项目,至少在理论上,这是一个制作起来非常简单的工具)。
我现在的问题是,当它位于 promise 的第二层时,我似乎无法让 console.log
显示任何输出。
此代码显示第一个 console.log
条目,但第二个条目消失在网络空间中。它不会显示任何错误或任何内容,只是不会出现在控制台中。
git.Repository.open(repo_root).then(function (repo_handle) {
repo_handle.Tag.list(repo_handle).then(function (tag_list) {
console.log("THIS SHOWS"); // <--- Shows
repo_handle.getTagByName(tag_list[0]).then(function (tag) {
console.log("THIS DOES NOT"); // <--- Doesn't show
});
});
});
为了验证问题不在 getTagByName
函数中,下面的代码工作正常并输出 THIS SHOWS
,所以这与将记录功能在 Promise 的第二层。
git.Repository.open(repo_root).then(function (repo_handle) {
repo_handle.getTagByName('v0.0.1').then(function (tag) {
console.log("THIS SHOWS");
});
});
顺便说一下,我尝试了同一代码的几个不同版本,例如使用 return repo_handle.Tag.list(repo_handle)
和 then(tag_list)
,但结果是相同的。
据我所知,代码实际上没有任何错误或任何东西,代码似乎工作得很好,因为我没有收到任何错误,但如果console.log
无法正常工作,那么可能只是它没有向我显示错误的情况...
最佳答案
这可能意味着 repo_handle.getTagByName('v0.0.1').then(function (tag) {
永远不会启动。尝试这样的操作:
git.Repository.open(repo_root).then(function (repo_handle) {
repo_handle.Tag.list(repo_handle).then(function (tag_list) {
console.log("THIS SHOWS"); // <--- Shows
repo_handle.getTagByName(tag_list[0]).then(function (tag) {
console.log("THIS DOES NOT"); // <--- Doesn't show
}).catch(error => {
console.log("gotcha! ", error);
});
});
});
console.log 本身与 Promise 嵌套的深度无关
来自提问者的更新
对于后来发现这一点的人来说,实际问题是 repo_handle.Tag
未定义,我通过为错误添加 catch
找到了它,因此接受这个答案。
新的更新代码如下:
let nodegit = require('nodegit');
let path = require('path');
var repo_root = path.resolve(__dirname, './.git');
let repo = null;
nodegit.Repository.open(repo_root)
.then(function (repo_handle) {
repo = repo_handle;
return nodegit.Tag.list(repo_handle);
})
.then(function (tag_list) {
return repo.getTagByName(tag_list[0]);
})
.then(function (tag) {
console.log(tag.message());
})
.catch(function (e) {
console.error(e);
});
关于javascript - Console.log 未显示在 Node promise 的第二层中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53429723/