This question is a followup to this other question.
[Should I explicitly reproduce the content here?]
Mind the fact that this is the firs time I learned about asynchronous code flow.
I read also about 'promises' in JavaScript.
I think I understood, at least in part, the idea in the answer. In very short terms, I need to put what I need to do with data.mml
in the callback function passed to typeset
.
Now, I am a bit stuck in how to organize the flow. This is what I need to do overall (which I didn't get to add in my previous question):
I have a string, say
var input = 'First $a^2=b$ second $a+b+c=d$ last';
I will split it
var splitted = input.split('$');
So we should get
splitted = ['First ', 'a^2=b', ' second ', 'a+b+c=d', ' last'];
And then odd elements of splitted
are the ones that need to be transformed into MathML by typeset
.
Say that we are about to process the first odd element a^2=b
. At some point in time typeset
will produce the corresponding MathML. Lets call it data
. With the idea I got from the linked answer I would need to put what I need to do with data
inside the call back function. What I need to do is to insert it as the second element of splitted
. There is some extra processing before inserting but let us skip that for the sake of clarity. So, just the insertion part. When processing a+b+c=d
it would be the fourth element of splitted.
My problem is that it seems that second element and fourth element are information that is external to typeset
. So, how would the called-back function know that the data
that was entered this time is the one corresponding to a^2=b
and not to a+b+c=d
.
Ah! Maybe it is relevant the fact that I don't have control over the signature of the callback function that should be passed to typeset
. From what I know the function has a single input which is data
containing the MathML.