简体   繁体   中英

handling return values in promises nodejs

I have built this function in NodeJs,

var Git = require('nodegit');
var fse = require('fs-extra');
var path = require('path');
var fs = require('fs');
var repoPath = 'D:\\sample'

function gitCommitHistory(repoPath, callbackGitCommitHistory) {
try {
    var open = Git.Repository.open;
    var commitList = [];
    open(repoPath)
      .then(function(repo) {              
        return repo.getMasterCommit();
      })
      .then(function(firstCommitOnMaster) {
        var history = firstCommitOnMaster.history();            
        history.on("commit", function(commit) {
            if (commit === 'undefined') {
                callbackGitCommitHistory(null, commitList);
            }
          var author = commit.author();
          commitList.push({commitAuthor:author.name(),commitAuthorEmail:author.email(), 
              commitMessage:commit.message(), commitSha:commit.sha(), commitDate:commit.date()});
        });
        history.on("error", function(err){
            console.log(err)
        })
        history.on("end", function(){               
            callbackGitCommitHistory(null, commitList);
        });
        history.start();
      });   
} catch (error) {
    callbackGitCommitHistory(error);
}

};

I have built this function using module "Nodegit". They are using promises as the callback handler.

In the function I am retrieving all the commits Done by a user on a repository and sending it as a response to the calling webservice.

The function works fine if there is atleast one commit, (ie) the repo.getMasterCommit returns the commit history. But if I give the repoPath to a new repo with zero commits then, nothing is returned from that function, hence i cannot send any response to the calling web service. Pls help on how to handle this situation!!!!!!!!

Related issue on GitHub repo.

That should be fixed in the next release (0.5) and is currently fixed in master via 661 . Ahmad Assaf is correct though, it was rejecting the promise chain since there's no head commit.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM