# concatAll

## Collect observables and subscribe to next when previous completes.

⚠ Be wary of backpressure when the source emits at a faster pace than inner observables complete!
💡 In many cases you can use concatMap as a single operator instead!

### Why use `concatAll`?

This operator is best used when you have multiple observables that need to be processed sequentially, ensuring that each observable completes before moving on to the next. Real-world examples of this can be seen in scenarios such as uploading multiple files to a server one-by-one, or displaying a sequence of animations in order.
Bear in mind that `concatAll` will only start processing the next observable when the current one completes. This is an important consideration if you have observables that emit values indefinitely or take a long time to complete, as it may cause a delay in processing subsequent observables.
Additionally, if you're working with observables that can emit values concurrently and don't need to wait for one to complete before processing another, mergeAll might be a more suitable choice. Similarly, if you only need to combine the values of multiple observables at the point when they all complete, forkJoin could be a better option.

### Examples

Example 1: concatAll with observable
// RxJS v6+
import { map, concatAll } from 'rxjs/operators';
import { of, interval } from 'rxjs';
//emit a value every 2 seconds
const source = interval(2000);
const example = source.pipe(
//for demonstration, add 10 to and return as observable
map(val => of(val + 10)),
//merge values from inner observable
concatAll()
);
//output: 'Example with Basic Observable 10', 'Example with Basic Observable 11'...
const subscribe = example.subscribe(val =>
console.log('Example with Basic Observable:', val)
);
Example 2: concatAll with promise
// RxJS v6+
import { map, concatAll } from 'rxjs/operators';
import { interval } from 'rxjs';
//create and resolve basic promise
const samplePromise = val => new Promise(resolve => resolve(val));
//emit a value every 2 seconds
const source = interval(2000);
const example = source.pipe(
map(val => samplePromise(val)),
//merge values from resolved promise
concatAll()
);
//output: 'Example with Promise 0', 'Example with Promise 1'...
const subscribe = example.subscribe(val =>
console.log('Example with Promise:', val)
);
Example 3: Delay while inner observables complete
// RxJS v6+
import { take, concatAll } from 'rxjs/operators';
import { interval, of } from 'rxjs';
const obs1 = interval(1000).pipe(take(5));
const obs2 = interval(500).pipe(take(2));
const obs3 = interval(2000).pipe(take(1));
//emit three observables
const source = of(obs1, obs2, obs3);
//subscribe to each inner observable in order when previous completes
const example = source.pipe(concatAll());
/*
output: 0,1,2,3,4,0,1,0
How it works...
Subscribes to each inner observable and emit values, when complete subscribe to next
obs1: 0,1,2,3,4 (complete)
obs2: 0,1 (complete)
obs3: 0 (complete)
*/
const subscribe = example.subscribe(val => console.log(val));
{"ft_lang_label":"__label__en","ft_lang_prob":0.70050186,"math_prob":0.9386817,"size":2781,"snap":"2023-40-2023-50","text_gpt3_token_len":656,"char_repetition_ratio":0.15520346,"word_repetition_ratio":0.1191067,"special_character_ratio":0.26357424,"punctuation_ratio":0.17738791,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.963166,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-09-28T16:40:34Z\",\"WARC-Record-ID\":\"<urn:uuid:c21c1fd8-d902-46d9-83d9-f95d28294e4b>\",\"Content-Length\":\"300529\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:ad813184-b67c-4ebc-b938-0c745c849f89>\",\"WARC-Concurrent-To\":\"<urn:uuid:d20d4523-0d2c-4efc-bac6-21301e21d391>\",\"WARC-IP-Address\":\"172.64.147.209\",\"WARC-Target-URI\":\"https://www.learnrxjs.io/learn-rxjs/operators/combination/concatall\",\"WARC-Payload-Digest\":\"sha1:GZKT5WY6N4FG5RJQYRHFN6JI74M6VHEK\",\"WARC-Block-Digest\":\"sha1:TALIURWHAKZCCJBA7MC5M6WJ5JDRPXYU\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-40/CC-MAIN-2023-40_segments_1695233510427.16_warc_CC-MAIN-20230928162907-20230928192907-00421.warc.gz\"}"}