【发布时间】:2020-10-09 00:22:18
【问题描述】:
我想在承诺完成后运行_this.install(),它似乎不知道什么是更好的方法?
main.js
async function gitSetUp(path, obj) {
const _this = obj
let myFirstPromise = new Promise((resolve, reject) => {
gitclass.cloneHttps(_this.props.appName)
setTimeout( function() {
resolve("Success!") // Yay! Everything went well!
}, 250)
})
myFirstPromise.then(function(res){
process.chdir(`${process.cwd()}/${_this.props.appName}`);
_this.install();
})
}
GitClass 代码
我最小化代码可能是一些变量没有正确映射只是为了理解promise逻辑
class GitClass {
cloneHttps(appName) {
nodegit.Clone.clone(url)
.then(function(repo) {
console.log(`Repo ${repo} cloned.`);
const data = {
cloneOptions: cloneOptions,
dir: DIR
}
return data;
}).then(function(data) {
return openRepo(data.dir, data.cloneOptions, appName);
})
.catch(function(err) {
console.log(err);
});
}
}
function openRepo(dir, cloneOptions, appName) {
//getMostRecent master branch commit
const getMostRecentCommit = function(repository) {
return repository.getBranchCommit('master').then(function(commit) {
repoData.commit = commit;
return repoData;
});
};
// create new branch
const createBranch = function(data) {
return data.repo.createBranch(data.branchName, data.commit, 1).then(function(res) {
data.res = res;
return data
});
}
// call main node function for promise chaining
return new Promise(resolve => {
nodegit.Repository.open(dir)
.then(getMostRecentCommit)
.then(createBranch)
.then(function(res) {
console.log("Promise Results", res);
resolve(res);
})
.catch(function(err) {
console.log("ERRR>>>>>>>", err);
});
})
}
【问题讨论】:
-
你有什么错误吗?
-
@losif gitClass.cloneHttps 正在创建新目录,安装作业将在其中完成,安装在导致大量错误的 git 操作之前运行
-
是什么让你怀疑 git 操作会在 250 毫秒内完成?
-
gitclass.cloneHttps 是否返回一个 Promise?
-
是的,它正在返回一个承诺
标签: javascript node.js promise