Find the closest node of certain type from a specific position

Hi people, I need to get the closest node of a particular type from a known position.

So let’s say I have the following nodes

image abc paragraph(1) paragraph(2)

I need to know from both paragraph which the closest abc node is in order to perform certain logic

So I was planning to loop recursively using nodeBefore method until I found the abc node, but just wondering if there is a better way.

Something like this should work.

let found  = -1
doc.nodesBetween(pos, doc.content.length, (node, pos) => {
  if (found > -1) return false
  if (isMyNode(node)) found = pos
if (found > -1) console.log("there's a node at", found)

Thanks worked like a charm!