nodejs 如何同步执行代码并获取返回值

我有异步代码段,这段代码其实是newPromise()的对象,但是我需要多次循环这个对象同时获取其中的值,代码如下:
module.exports=function(options){
varnodegit=require("nodegit");
varconfigs=[
{
"path":"path1",
"branch":""
},
{
"path":"path2",
"branch":""
}
];
for(vari=0;inodegit.Repository.open(configs[i].path).then(function(repo){
returnrepo.getCurrentBranch();
}).then(function(reference){
configs[i].branch=reference.name();
returnreference;
});
}
console.log(configs);
};
上面的代码的意思是根据configs.path来更新configs.branch;但是nodegit使用的是Promise同步执行模式,请问我如何能获取Promise.then的数据,或者上面的代码如何修改比较合适?谢谢!
难道我需要通过循环一层一层的then下去?
杨__羊羊
浏览 2202回答 2
2回答
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript