0

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.

Community
  • 1
  • 1
myfirsttime1
  • 287
  • 2
  • 12
  • Possibly what you need : http://stackoverflow.com/questions/5044807/passing-scope-to-callback-function-binding – phenxd Feb 23 '16 at 17:44
  • @phenxd Let me see. Sounds similar. – myfirsttime1 Feb 23 '16 at 17:46
  • @phenxd Yes, this solved the problem, part of it. By defining the anonymous callback function in the same scope as an extra variable, I can access the latter inside the callback function, and effectively retrieve an output out of the callback function. It is not the end of my problem, as I still need to figure out how to loop asyncronously through the elements of `splitted`. Right now the transformation is working and it is getting out. But the loop is not assigning them until the end of the loop. But that is a different question. Thanks. – myfirsttime1 Feb 23 '16 at 18:44

0 Answers0