Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/371.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在得到api响应之前停止函数,然后继续执行其余代码_Javascript_Angular - Fatal编程技术网

Javascript 在得到api响应之前停止函数,然后继续执行其余代码

Javascript 在得到api响应之前停止函数,然后继续执行其余代码,javascript,angular,Javascript,Angular,我真的有这个问题。 im使用的函数是为数组的每个组件检查一个计数器,仅当计数器小于数字y时,才将数组推送到变量 这里的问题是,当计数器达到极限时,我将变量发送到api,在成功的响应中,我重新启动计数器并清空变量以重新执行 我的问题是,当我发送第一个请求时,api需要一段时间才能给我一个响应,但数组的其余部分im检查已经过去,因为我只重置了响应中的计数器,只参考了一次api 我当前使用的代码是: cargaLotes(){ 让totallengthchange=this.Arraydata.len

我真的有这个问题。 im使用的函数是为数组的每个组件检查一个计数器,仅当计数器小于数字y时,才将数组推送到变量

这里的问题是,当计数器达到极限时,我将变量发送到api,在成功的响应中,我重新启动计数器并清空变量以重新执行

我的问题是,当我发送第一个请求时,api需要一段时间才能给我一个响应,但数组的其余部分im检查已经过去,因为我只重置了响应中的计数器,只参考了一次api

我当前使用的代码是:

cargaLotes(){
让totallengthchange=this.Arraydata.length;
this.cargaloader=true;
这一步=真;
for(此.Arraydata的常量值){
检查(值);
}
//this.lenghtloader=this.Arraydata.length;
让我们一起推;
让长度分开;
让长度显示;
const-porcetaje=1;
const totaldisplay=100;
让lengtharray=this.Arraydata.length;
const lengtharrastatico=this.Arraydata.length;
const temp:any=this.Arraydata;
如果(this.Step==true){
设contador1=0;
(续1<500){
每小时温度(数据=>{
如果(contador1<499){
this.arraytosend.push(数据);
contador1=contador1+1;
如果(contador1==长度阵列){
this.service.(service)(this.arraytosend).subscribe(resp=>{
this.notification.showNotification('top','right','Se Cargaron los Productos','warning');
这个.getProductoPrice();
this.lenghtloader=100;
},err=>{
this.notification.showNotification('top','right','Ocurrio un error','danger');
});
}
如果(contador1==499){
this.arraytosend.push(数据);
this.service.(service)(this.arraytosend).subscribe(resp=>{
控制台日志(resp);
总长度变化=总长度变化-500;
推后长度=总长度变化;
控制台日志(lengthafterPush);
lengthdivided=后推长度/后推长度;
长度显示=porcetaje-长度分割;
this.lenghtloader=lengthdisplay*totaldisplay;
},err=>{
控制台日志(err);
this.notification.showNotification('top','right','Ocurrio un error','danger');
});
lengtharray=lengtharray-500;
this.arraytosend=[];
contador1=0;
}
}
});
}
}否则{
this.ArraydataDuplicates=[];
for(此.Arraydata的常量值){
此。显示重复项(值);
}
这个._snackBar.open('El Archivo Contiene Productos que ya han sido registordos!,'Ocultar'{
持续时间:5000
});
}
}

简单地说,您的代码将值缓冲到数组中,然后将其发送到API。这将重复,直到数组中没有剩余值。您正在向API发送批量值

我将在下面给出一个非常简单的示例,说明您可以使用的模式。我忽略了代码正在执行的许多副作用——一旦对主模式进行了排序,这些副作用应该足够简单,可以集成

组件技术

private batchSize=500;
私人资料:编号【】;;
//处理一些事件,这些事件决定何时发送批
onSend(){
//假设此.data已设置在某个位置
this.sendBatches().subscribe(()=>{
console.log('done');
});
}

private sendBatches():可观察的签出承诺。你能不能提供一个stackblitz的演示,最好用最少的代码。可观测数据非常强大,所以你可以用它们做任何你想做的事情。我们只需要先了解你想要达到的目标。只需要几点帮助你写问题,这样任何人都可以立即理解问题,这会增加立即回答的机会:1。用短语提问,如果其他人有相同的问题,他们可以使用相同的问题。2.将代码放在小部分中,并解释每个部分的作用和应该做什么。在这个问题中,您提到了API请求。清楚、清晰地显示代码,并可能添加注释,以说明如何生成API请求。在这个问题中,您提到了计数器以及计数器的其余部分。明确代码在哪里,休息在哪里,以及您认为应该暂停的地方