【发布时间】:2016-08-14 12:40:15
【问题描述】:
我正在构建一个使用 nodegit npm 包的 nodeJS 应用程序。因此,根据他们文档中的示例,我有以下章节。它使用一系列承诺,看起来像一个 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");
});
}
因此,将 console.log 放入 history.on() 事件处理程序会显示我想要查看的所有信息。所以我对我的阵列推动相当有信心。那么如何让prepareDiffs() 函数返回填充的results 数组或至少一个解析为数组的承诺?
注意:我使用的是针对 es6 的 Typescript,所以 async/await 是可用的。
【问题讨论】:
标签: javascript jquery node.js typescript