rxjs - Angular 2 : merge several http calls into one -
i have service used upload pictures. upload picture,
return this.http.post(/* ... */)
and subscription can subscribe in component. when want upload several pictures, have
for (let p of pics) { this.http.post(/* ... */); }
my problem return results of all calls instead of 1 call. possible ?
edit here service
addpictures(files: file[], folder: string): observable<parse.object[]> { let haserror = false; (let file of files) { let [type, ext] = file.type.split('/'); if (type.tolowercase() !== 'image' || !environment.imgexts.includes(ext.tolowercase())) { haserror = true; } } if (haserror) { return observable.throw('invalid extension detected'); } let observables: observable<parse.object>[] = []; (let file of files) { // size let img = new image(); img.onload = () => { // create parse document let parseimg = { url: '', type: file.type, width: img.width, height: img.height }; // upload on amazon , add db observables.push(this.addpicture(parseimg, file, folder)); } img.src = window.url.createobjecturl(file); } return observable.forkjoin(observables); }
if want run requests in parallel can use forkjoin()
:
const observables = pics.map(p => this.http.post(/* ... */)); observable.forkjoin(observables) .subscribe(results => ...);
you use observable.merge()
receive results arrive or observable.concat()
call requests 1 after another.
maybe have @ sending multiple parallel http requests , sending multiple sequential http requests
Comments
Post a Comment