---
title: "merge(with:)"
framework: combine
role: symbol
role_heading: Instance Method
path: "combine/publisher/merge(with:)-7qt71"
---

# merge(with:)

Combines elements from this publisher with those from another publisher, delivering an interleaved sequence of elements.

## Declaration

```swift
func merge<P>(with other: P) -> Publishers.Merge<Self, P> where P : Publisher, Self.Failure == P.Failure, Self.Output == P.Output
```

## Parameters

- `other`: Another publisher.

## Return Value

Return Value A publisher that emits an event when either upstream publisher emits an event.

## Discussion

Discussion Use merge(with:) when you want to receive a new element whenever any of the upstream publishers emits an element. To receive tuples of the most-recent value from all the upstream publishers whenever any of them emit a value, use combineLatest(_:). To combine elements from multiple upstream publishers, use zip(_:). In this example, as merge(with:) receives input from either upstream publisher, it republishes it to the downstream: let publisher = PassthroughSubject<Int, Never>() let pub2 = PassthroughSubject<Int, Never>()

cancellable = publisher     .merge(with: pub2)     .sink { print("\($0)", terminator: " " )}

publisher.send(2) pub2.send(2) publisher.send(3) pub2.send(22) publisher.send(45) pub2.send(22) publisher.send(17)

// Prints: "2 2 3 22 45 22 17" The merged publisher continues to emit elements until all upstream publishers finish. If an upstream publisher produces an error, the merged publisher fails with that error.

## See Also

### Republishing elements from multiple publishers as an interleaved stream

- [merge(with:)](combine/publisher/merge(with:)-7fk3a.md)
- [merge(with:_:)](combine/publisher/merge(with:_:).md)
- [merge(with:_:_:)](combine/publisher/merge(with:_:_:).md)
- [merge(with:_:_:_:)](combine/publisher/merge(with:_:_:_:).md)
- [merge(with:_:_:_:_:)](combine/publisher/merge(with:_:_:_:_:).md)
- [merge(with:_:_:_:_:_:)](combine/publisher/merge(with:_:_:_:_:_:).md)
- [merge(with:_:_:_:_:_:_:)](combine/publisher/merge(with:_:_:_:_:_:_:).md)
