我正在構建一個使用nodegit
npm包的nodeJS應用程序。因此,根據他們的文檔中的示例,我有以下章節。它使用一系列的promise,看起來像一個JQuery事件處理程序。這裏是我的功能:如何從承諾中的事件處理程序返回值?
export function prepareDiffs() {
var patt = new RegExp('[0-9].[0-9]');
var results: Array<Commit> = [];
Git.Repository.open("features/tutorial") // Open the repository directory.
.then(function (repo) { // Open the master branch.
return repo.getMasterCommit();
})
.then(function (firstCommitOnMaster) { // Display information about commits on master.
var history = firstCommitOnMaster.history(); // Create a new history event emitter.
history.on("commit", function (commit) { // Listen for commit events from the history.
var entry = new Commit();
entry.hash = commit.sha();
var step = patt.exec(commit.message());
if (step !== null) {
entry.step = step.toString();
}
results.push(entry);
})
history.start(); // Start emitting events.
console.log("return");
});
}
所以在history.on()
事件處理程序將執行console.log顯示了所有的信息,我想看到的。所以我對陣列的推動很有信心。所以我怎樣才能得到prepareDiffs()
函數返回填充的results
數組或至少一個承諾解決陣列?
注意:我正在使用Typescript定位es6,因此async/await可用。