Coder Social home page Coder Social logo

Async Iterators about fluture HOT 4 CLOSED

ftaiolivista avatar ftaiolivista commented on June 10, 2024
Async Iterators

from fluture.

Comments (4)

Avaq avatar Avaq commented on June 10, 2024 1

Ah. I realize I've misunderstood your original question. I'm glad you've found a solution. I can't really see a way to work around the problem of that trailing Future: You have to yield synchronously because Infinite only works with synchronous generators, and you only find out that the underlying async generator was done some ticks later, after having already yielded that Future.

from fluture.

Avaq avatar Avaq commented on June 10, 2024

Hi @ftaiolivista. I answered a very similar question some time ago. It was in a GitHub issue, so not that easy to discover. I converted it to a Q&A Discussion. I think it might be what you need: #478

from fluture.

ftaiolivista avatar ftaiolivista commented on June 10, 2024

Thank you @Avaq for the quick reply. Looked ad #478. Looks clear how to wrap a standard generator with Lazy List but not clear to me on how to wrap an async iterator that yield promises.

from fluture.

ftaiolivista avatar ftaiolivista commented on June 10, 2024

@Avaq got it working this way. The problem is that I have one last future with undefined value because the Infinite loop cancellation happen after one last promise with undefined value

import { S, F } from './src/lib.js' // S = sanctuary F = Fluture
import { createReadStream, promises as FS } from 'fs'
import parse from 'csv-parse'
import Infinite from 'lazy-infinite'

const parsing = createReadStream('file.csv').pipe(parse())

const infinite = asynciterable =>
    Infinite.of(function * () {
        const i = asynciterable[Symbol.asyncIterator]()
        let goon = true
        do {
            yield F.attemptP(() =>
                i.next().then(({ done, value }) => {
                    goon = !done
                    return done ? undefined : value
                })
            )
        } while (goon)
    })

S.pipe([
    infinite,
    F.map(
        F.chain(v => {
            console.log('process', v)
            return F.after(2000)(v)
        })
    ),
    i => F.go(i.gen),
    F.fork(err => console.error('err', err))(console.log)
])(parsing)

from fluture.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.