You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm doing a live preview. I find code nodes, do the compilation, save the content to the file, then replace the code node for a custom node (transforms into a <script> tag) pointing to the file. Then I parse it with rehype.
Yes, currently it does "work" because I return a future filename immediately, but I'm unable to catch errors and propagate them "higher".
I was thinking about it just a little bit more and a workaround could look like this:
asyncfunctiontransform(tree){constmatches=[];visit(tree,'code',visitor)functionvisitor(node,index,parent){matches.push({ node, index, parent })}constpromises=matches.map(asyncmatch=>{constfilename=awaitsomeAsyncStuff(match.node.value)// ... modify tree ...})awaitPromise.all(promises);returntree;}
But I'm not sure, how exactly does visit work, because this could cause issues with removing notes (indexes might get out of sync). But it might be a case anyway.
I think your new example is indeed (almost) the way to go: otherwise, you would do a waterfall. You’d only be able to process serially, but with that new approach, you can send out all requests immediately!
The point about the indexes is true! But you can work around that: you need the parent, and the node. And then you can do parent.children[parent.children.indexOf(node)] = replacement!
Of course one can do indexOf oneself. I was thinking about the drawbacks of the solution ... whether the request is still valid. Now I think a wrapper function is more suitable. So I'll just leave here the code, in case someone else would be searching for this:
awaitvisitAsync(tree,'code',visitor)asyncfunctionvisitor(node,index,parent){// ! Note: Index is not guaranteed, in case of adding/removing nodes// ... async stuff here ...}
Thanks for your assistance!
Reacted by Titus, Thibaut Patel, la3rence and andrew sòlanto
You cannot really correct the index, can you? You would have to do it after the async stuff, but that is out of your control. Or am I missing something?
I'm not going to make it into utility, but please feel free to do it yourself.
You are right about index. I kept it there because in my case, I was replacing nodes, so it was convenient. However, without return tree, visit() was reporting several notes multiple times.
UPDATE: Came across the[next()](https://github.com/unifiedjs/unified#function-nexterr-tree-file) param when I carefully scanned the docs again. It was the missing piece for me from @tomasbonco example ^ which helped as a hint.
The key is to call next() once when all is complete. I'm doing that by tracking the promises in an array and calling next() once when all the promises have resolved, using Promise.all().
I'll try and write something up and make a contribution to improve the docs.
It's not clear to me how one transforms a doc using async code. I need to process markdown docs in order to translate code blocks. Given there isn't a plugin to do this I'm trying to write one. In the transformer plugin I need to do something like node.value = await thirdpartyTranslateFunc(node.value). The third party convert function is async. My code looks like below. This works when I change node.value with sync code. With async it seems the compiler completes before the async code finishes.
How do I solve for this?
I'm probably missing something about how unified works.
constmyvisitor=(node: any)=>{if(node.lang&&node.lang=='X'){// run async code to convert from X to Y// set node.value to Y}elseif(node.lang==null){node.lang='shell-session'}//console.log(node);//console.log("====");}exportconstmycodeconverterPlugin=(_options: any,_set: any)=>{return(tree: any,_file: any)=>{visit(tree,'code',myvisitor);};}constbuffer=fs.readFileSync('something-file.markdown');consts=awaitremark().use([mycodeconverterPlugin]).process(buffer);
@janaka You don’t need next. That’s callback style. Which sure you can use. But you can also return promises.
As for how to do async work, split the work up in two stages: a) find things, b) transform things. Such as like this:
/** * @typedef {import('mdast').Root} Root * @typedef {import('mdast').Code} Code */importfsfrom'node:fs/promises'import{remark}from'remark'import{visit}from'unist-util-visit'constfile=awaitremark().use(remarkConvertMyCode).process(awaitfs.readFile('something-file.markdown'))console.log(String(file))/** * @type {import('unified').Plugin<[], Root>} */functionremarkConvertMyCode(){returnasyncfunction(tree){/** @type {Array<Code>} */constcodes=[]visit(tree,(node)=>{if(node.type==='code'){codes.push(node)}})/** @type {Array<Promise<void>>} */constpromises=[]for(constcodeofcodes){if(code.lang&&code.lang==='X'){// Run async code to convert from X to Y// set node.value to Ypromises.push(somePromise)}elseif(code.lang===null){code.lang='shell-session'}}awaitPromise.all(promises)}}
Reacted by Connel Hooley
Reacted by Janaka Abeywardhana, sunhwang, abdellah hariti, Ikhsan Satriadi, Reza Zahedi and Oteto
I’m not sure a longer readme will improve this, what I believe to be, a more general “how to do async programming”, question (see my comment “split the work up in two stages: a) find things, b) transform things”)
The cases in this issue are each more practical, specific to a particular use case (code highlighting for example). I’m not sure how useful adding several examples of practical cases will be to readers with different use cases.
Activity
wooorm commentedon Apr 24, 2020
Can you go a level higher? What do you practically want to do?
tomasbonco commentedon Apr 24, 2020
I'm doing a live preview. I find
codenodes, do the compilation, save the content to the file, then replace thecodenode for a custom node (transforms into a<script>tag) pointing to the file. Then I parse it with rehype.Yes, currently it does "work" because I return a future filename immediately, but I'm unable to catch errors and propagate them "higher".
tomasbonco commentedon Apr 24, 2020
I was thinking about it just a little bit more and a workaround could look like this:
But I'm not sure, how exactly does
visitwork, because this could cause issues with removing notes (indexes might get out of sync). But it might be a case anyway.wooorm commentedon Apr 24, 2020
I think your new example is indeed (almost) the way to go: otherwise, you would do a waterfall. You’d only be able to process serially, but with that new approach, you can send out all requests immediately!
The point about the indexes is true! But you can work around that: you need the parent, and the node. And then you can do
parent.children[parent.children.indexOf(node)] = replacement!tomasbonco commentedon Apr 25, 2020
Of course one can do
indexOfoneself. I was thinking about the drawbacks of the solution ... whether the request is still valid. Now I think a wrapper function is more suitable. So I'll just leave here the code, in case someone else would be searching for this:Example usage:
Thanks for your assistance!
wooorm commentedon Apr 28, 2020
If you’re making this into a utility, then it would make sense to correct
indexin there, no?I think this is the solution to go with, no need to change this project. Thanks!
tomasbonco commentedon Apr 29, 2020
You cannot really correct the
index, can you? You would have to do it after the async stuff, but that is out of your control. Or am I missing something?I'm not going to make it into utility, but please feel free to do it yourself.
wooorm commentedon Apr 29, 2020
Oh, right, I was thinking this would somehow run the promises serially instead of all at once, in which case
parent.indexOf(node)would do the trick.If you can’t make
indexcorrect, I suggest not passing it, e.g.,:Btw
return treein the visitor shouldn’t be there.tomasbonco commentedon Apr 29, 2020
You are right about
index. I kept it there because in my case, I was replacing nodes, so it was convenient. However, withoutreturn tree,visit()was reporting several notes multiple times.wooorm commentedon Apr 29, 2020
The last part is weird! The return value of a node isn’t handled: https://github.com/syntax-tree/unist-util-visit-parents#returns 🤔 Maybe your arrow function was without braces, with push, and thus return an index (
pushreturns a number)?tomasbonco commentedon Apr 29, 2020
That is exactly what happened. I wasn't aware of that behavior. Thank you!
shtse8 commentedon Jun 22, 2022
any updates about async visitor? it is a big need!
wooorm commentedon Jun 22, 2022
No, it is not needed. It is an anti pattern and it is very slow.
janaka commentedon Feb 1, 2023
UPDATE: Came across the
[next()](https://github.com/unifiedjs/unified#function-nexterr-tree-file)param when I carefully scanned the docs again. It was the missing piece for me from @tomasbonco example ^ which helped as a hint.The key is to call
next()once when all is complete. I'm doing that by tracking the promises in an array and callingnext()once when all the promises have resolved, usingPromise.all().I'll try and write something up and make a contribution to improve the docs.
It's not clear to me how one transforms a doc using async code. I need to process markdown docs in order to translate code blocks. Given there isn't a plugin to do this I'm trying to write one. In the transformer plugin I need to do something like
node.value = await thirdpartyTranslateFunc(node.value). The third party convert function is async. My code looks like below. This works when I changenode.valuewith sync code. With async it seems the compiler completes before the async code finishes.How do I solve for this?
I'm probably missing something about how unified works.
wooorm commentedon Feb 2, 2023
@janaka You don’t need
next. That’s callback style. Which sure you can use. But you can also return promises.As for how to do async work, split the work up in two stages: a) find things, b) transform things. Such as like this:
janaka commentedon Feb 2, 2023
@wooorm ah, I see, cool. I was close but can clean up my code. Thanks for the example.
a-hariti commentedon Mar 6, 2024
Thank you @wooorm.
This is a much needed example, it should probably figure on the main README somewhere
wooorm commentedon Mar 7, 2024
I’m not sure a longer readme will improve this, what I believe to be, a more general “how to do async programming”, question (see my comment “split the work up in two stages: a) find things, b) transform things”)
The cases in this issue are each more practical, specific to a particular use case (code highlighting for example). I’m not sure how useful adding several examples of practical cases will be to readers with different use cases.
You can also recommend recipes to https://unifiedjs.com/learn/.