Skip to content

Support for async visitors #8

Closed
@tomasbonco

Description

@tomasbonco

In my opinion, this should be working:

await visit( tree, 'code', visitor )

function visitor( node, index, parent ) {
    return new Promise( resolve => {
        const removeNode = () => {
            parent.children.splice( index, 1 );
            resolve()
        }
		
        setTimeout( removeNode, 10 );
    })
}

Another (more abstract) example:

await visit( tree, 'code', visitor )

async function visitor( node, index, parent ) {
   node.value = await someAsyncStuff();
}

Background

Visitor might do:

  1. read a node
  2. perform an action based on the node value/props (reading/writing files, do a HTTP request, ...)
  3. modify the tree based on the result of async operation

However, currently, only sync operations are allowed. That's why it cannot be used for async transformers, which should be supported by unified.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions