As a "branch" question from my other question (see Edit 4) How to convert a png base64 string to pixel array without using canvas getImageData?
I wonder what happens when you have a set of functions like this:
function convertTileToRGB(tile,whatToDoNext){
tile.img.onload = function() {
//async code returns pixels
whatToDoNext(pixels);
}
}
function doSomethingWithTile(tile,params){
convertTileToRGB(tile,function(pixels){
//manipulate pixels in tile
});
}
function doSomethingWithTile2(tile,params){
convertTileToRGB(tile,function(pixels){
//manipulate pixels in a different way
});
}
Is there a way a arbitrary call sequence like this,
var tile = initialize_tile();
doSomethingWithTile(tile,params1)
doSomethingWithTile2(tile,params2)
doSomethingWithTile(tile,params3)
...
that is dynamic and not known in advance, in other words added asynchronously to a dispatcher while the program is running and then the dispatcher is invoking them synchronously, to be solved somehow?
If we don't have a dispatcher and we use callbacks we can see that the methods act separately on the initialized tile but their actions are not accumulated to the tile. In that example order is preserved but essentially the result of the previous async method is not used in the next one.
Thanks in advance for any help
EDIT: thanks for the answers. yeah order matters but this list of things is being changed throughout the time(more things are added) and is not known before hand. It would be nice if there was a way to have a dispatcher object that will have what is to be done in a specific order being added asynchronously but it will invoke them in that order synchronously. I changed my question accordingly because most of the people were confused.
EDIT2: Trying to change my code: The dispatcher would look like below. The tile and sample could be different for each invocation but could be the same for some. Other actions will be applied except for addSample. If the same tile is used the invocations changes should be accumulated in the tile in that order. And most importantly the dispatcher should be able to get more to do actions asynchronously.
var sample = [[0x7f,0x7f,0x7f],[0x7f,0x7f,0x7f],[0x7f,0x7f,0x7f],[0x7f,0x7f,0x7f],[0x7f,0x7f,0x7f],[0x7f,0x7f,0x7f],[0x7f,0x7f,0x7f],[0x7f,0x7f,0x7f],[0x7f,0x7f,0x7f],[0x7f,0x7f,0x7f],[0x7f,0x7f,0x7f],[0x7f,0x7f,0x7f],[0x7f,0x7f,0x7f]]
var tile = initialize_tile({width: 7,height: 7,defaultColor: [0x00,0xAA,0x00], x: 1, y: 2, zoomLevel: 1});
var dispatcher = {
0:{
func: addSample,
tile: tile,
sample: sample,
num_samples: 6,
which_sample: 1
},
1:{
func: addSample,
tile: tile,
sample: sample,
num_samples: 6,
which_sample: 2
},
2:{
func: render,
tile: tile,
}
};
var disp = dispatcher[0];
disp.func(disp.tile,disp.sample,disp.num_samples,disp.which_sample);
disp = dispatcher[1];
disp.func(disp.tile,disp.sample,disp.num_samples,disp.which_sample);
With that code the order is not preserved and what happens to the tile is not kept for the next invocation.