Skip to content

Support for AsyncIterator #150

@chr15m

Description

@chr15m

Occasionally an API requires the use of a JavaScript async for over some iterator like this:

for await (const [key, value] of this.keyv.iterator()) {
      console.log(key, value); // Return: key and value 
};

It would be neat if there was a way to do this directly in promesa, similar to the macro at How to work with asynciterable. Here's the macro:

(defmacro async-doseq [[binding async-iterable] & body]
  `(let [async-iterator# ((js* "~{}[Symbol.asyncIterator]" ~async-iterable))]
     (fn handle-next# []
       (let [next# (.next async-iterator#)]
         (.then 
          next#
          (fn [res#]
            (if (.- res# done)
              nil
              (do (let [~binding (.- res# value)]
                    ~@body) 
                  (handle-next#)))))))))

Is this something Promesa is interested in supporting?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions