大佬教程收集整理的这篇文章主要介绍了Typescript Promise 循环中的数据流和内存问题,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在处理一个项目,我必须从 Azure DevOps 存储库中读取大约 3000 多个 JsON 文件,并将它们的内容存储在一个字符串中,以便稍后使用。
以下是我用来获取存储库中项目列表的方法。我不允许假设任何目录层次结构,因此我将 recursionLevel
参数设置为完整。
文档链接:https://docs.microsoft.com/en-us/javascript/api/azure-devops-extension-api/gitrestclient#getitems-String--String--String--versioncontrolrecursiontype--Boolean--Boolean--Boolean--Boolean--gitversiondescriptor-
function getItems(repositoryID: String,project?: String,scopePath?: String,recursionLevel?: VersionControlRecursionType,includeContentMetadata?: Boolean,latestProcessedChange?: Boolean,download?: Boolean,includelinks?: Boolean,versionDescriptor?: GitVersionDescriptor)
上面的函数返回一个 Promise of Git 项目列表,从中我可以从存储库的根目录获取每个文件的相对路径
获得项目列表后,我想一个一个地阅读它们并将它们的内容存储在一个字符串中。为此,我正在使用这种方法:
function getItemText(repositoryID: String,path: String,versionDescriptor?: GitVersionDescriptor,includeContent?: Boolean,resolveLfs?: Boolean)
这个功能也可以在我之前分享的同一个链接中使用。你必须向下滚动一点。 它将内容作为字符串的 Promise 返回。
我读取所有文件的代码:
AdoClIEnt.geTinstance().getItems(this.repoID,this.projectname,this.commonData.inputBranchName).then(data=>{
data.forEach(element => {
if(element.gitObjectType==3)
{
AdoClIEnt.geTinstance().getItemText(this.repoID,element.path,this.commonData.inputBranchName).then(element=>{ content.concat(element);
});})});
在上面的代码中:
这是 AdoClIEnt 文件中的原始函数调用:
public async getItemText(repoID: String,filePath: String,branchname: String) {
return this.gitClIEnt.then(clIEnt => clIEnt.getItemText(repoID,filePath,null,'none',false,{version: branchname,versionoptions: 0,versionType: 0}));
}
public async getItems(repositoryID: String,project: String,branch: String){
return this.gitClIEnt.then(clIEnt=>clIEnt.getItems(repositoryID,project,"/serviceGrouproot/",'full',true,{version: branch,versionType: 0}));
}
另请注意,/serviceGrouproot/
是我存储库中的根目录。
执行此操作时,我无法获取内容变量中的所有内容,而且由于同时进行了 3000 多次 AJAX 调用,Google Chrome 还显示 ERR_INSUFFICIENT_resourcES
。这个错误不会出现在 Mozilla 中。但是在 Mozilla 中,当我打印内容时,它显示空字符串。我想要的是一种对我的执行步骤进行排序的方法,以便我可以在我的内容字符串中拥有所有内容。有没有办法运行一个 Promise 调用循环并让它们一个一个执行?一个接一个我的意思是完成一个然后开始下一个而不是一次加载所有内容还是我必须遵循其他一些方法?
我想最简单的方法是使用带有 await 的 for 循环:
const work = i => {
return new Promise(resolve => {
setTimeout(() => resolve(i),Math.floor(Math.random() * 100));
});
};
const array = Array(10).fill(0).map((_,i) => i + 1);
// non sequencial
const test_then = () => {
array.forEach(i => {
work(i).then(i => {
console.log(i);
});
});
};
// sequencial
const test_await = async () => {
for (let i = 0; i < array.length; i++) {
console.log(await work(i));
}
};
test_then();
test_await();
,
代码中有一些严重错误。
建议使用传统的 for 循环,以便我们可以使用 async await 语法。
我不知道资源部分,但我在下面粘贴了清晰的代码。
async function main() {
let content = '';
const data = await AdoClient.geTinstance().getItems(/* pass params here*/);
for (let i = 0; i < data.length; i++) {
if(data[i].gitObjectType==3){
const result = await AdoClient.geTinstance().getItemText(/* pass params here*/);
content.concat(result);
}
}
return content;
} // main close
const c = main();
console.log(c);// your output
以上是大佬教程为你收集整理的Typescript Promise 循环中的数据流和内存问题全部内容,希望文章能够帮你解决Typescript Promise 循环中的数据流和内存问题所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。