2

I'm creating a custom CHANGELOG generator based on git commits using nodegit (I didn't quite like the existing similar projects, and it's a pretty simple tool to make, in theory at least).

My problem right now is that I can't seem to get the console.log to show any output when it's in the second layer of promises.

This code shows the first console.log entry, but the second one disappears into cyberspace. It doesn't show any errors or anything, it just doesn't appear in the console.

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

And just to verify that the problem isn't in the getTagByName function, the below code works just fine and outputs THIS SHOWS, so it's something related to putting the logging function in the second layer of the promises.

git.Repository.open(repo_root).then(function (repo_handle) {
  repo_handle.getTagByName('v0.0.1').then(function (tag) {
    console.log("THIS SHOWS");
  });
});

I've tried a couple different versions of the same code by the way, e.g. using return repo_handle.Tag.list(repo_handle) and then(tag_list), but the results were the same.

As far as I can tell the code isn't actually having any errors or anything, the code seems to work just fine since I'm not getting any errors, but then again if console.log isn't working properly then it could just be the case that it isn't showing me the errors either...

Simon Hyll
  • 3,265
  • 3
  • 24
  • 44
  • `then` only fires if your promise is successful, so your promise isn't successful. You don't actually show what `getTagByName` does so beyond that we can't help – Liam Nov 22 '18 at 11:16
  • 1
    offtopic: nested `.then()` is anti-pattern, they are supposed to be chained, not forming another callback hell – William Chong Nov 22 '18 at 11:18
  • @Liam getTagByName is part of the `nodegit` API, so if you wanna see it you'd have to head on over there. It's an async function that returns a `Tag` object. – Simon Hyll Nov 22 '18 at 11:26
  • @William Chong Now that you mention it I know I've read about that before and actually know about it... I'm gonna pretend this was just test code that just needed to work, not be good ^^ – Simon Hyll Nov 22 '18 at 11:28
  • off topic, did you know you surname means "ugly" in welsh? Not to make any comment on your character or person but thought you might find it interesting :) – Liam Nov 22 '18 at 11:33
  • @Liam What seriously?? Haha that's random xD – Simon Hyll Nov 22 '18 at 11:37
  • Yep seriously https://forvo.com/word/hyll/ "ll" is a character in welsh and has an unusual pronunciation, if you press the play icon on that link you'll see what I mean. – Liam Nov 22 '18 at 11:38
  • In Swedish "hyll" is 1 letter from "hylla" which means "shelf", so people joke about us being the shelfs, but now I guess we can be the ugly shelfs... And we're Swedish... WE'RE [BILLY THE BOOKCASE](https://www.ikea.com/se/sv/catalog/products/S59182201/) FROM IKEA!! xD – Simon Hyll Nov 22 '18 at 11:40

1 Answers1

2

It probably means that repo_handle.getTagByName('v0.0.1').then(function (tag) { never starts. Try something like this:

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 itself has nothing to to with the depth of promise nesting

Update from asker

For those that find this later, the actual problem turned out to be that repo_handle.Tag was undefined, and I found it thanks to adding a catch for the error, hence accepting this answer.

The new updated code that works is as follows:

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);
  });
Simon Hyll
  • 3,265
  • 3
  • 24
  • 44
Anton Pastukhov
  • 588
  • 2
  • 11
  • I feel so stupid for not thinking of using a `.catch` for errors... It turns out there's some problem with `repo_handle.Tag.list` being undefined, even if I swear to Jebus that there are no errors in the code in that part since I used that part like 30 minutes ago fully functional... Will accept your answer since it lead to the solution, thanks! – Simon Hyll Nov 22 '18 at 11:24