Skip to content

Instantly share code, notes, and snippets.

@jj11hh
Last active May 19, 2020 11:28
Show Gist options
  • Select an option

  • Save jj11hh/d2234f20d6e570442ce702e257ca9357 to your computer and use it in GitHub Desktop.

Select an option

Save jj11hh/d2234f20d6e570442ce702e257ca9357 to your computer and use it in GitHub Desktop.
convert decimal numbers to hexadecimal asynchoronously
function xxx_rpc_call(x, callback){
setTimeout(callback, 1000 + Math.random()*1000, x.toString(16));
}
const results = new Array();
let index = 1;
function cb_arrange(x){
return (result) => {
results[x] = result;
if (x == 1 || results[x - 1] !== undefined){
if (x === index){
while (results[index] !== undefined){
console.log(`${index} -> ${results[index]}`);
index++;
}
}
}
};
}
for (let i = 1; i <= 30; i ++){
xxx_rpc_call(i, cb_arrange(i));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment