Sync api or keyword

We need a simple api permitting to convert a async function to a synchronous function. Example:

async function toBlob(file) {
 var r= await (new Promise(function(s,e){
 var reader = new FileReader();

reader.onloadend = function(e) {
  s(new Blob([ this.result ], { type: file.type } ));
};

reader.readAsArrayBuffer(file);
}))
return r;
}

sync(toBlob(file)) return directly the blob 
//use blob in synchronous way

Sync functions block the main thread locking up the browser UI, and cannot use multi-core CPUs so have a much lower performance limit. It’s much better to write async code, and new language features like await make it easier since your code looks more like sync code.

Could you explain why async/await doesn’t work for you?

let blob = await toBlob(file);

I haven’t tried it myself, but I think “switching to async” should be as easy as wrapping your program in an immediately-invoked async function.

1 Like

because once apon a time there a man told synchronous is different from asynchronous

What?

1 Like

we know it. but this one doesnt mean you can t use it in special block code. There is not a wrong api , there is a wrong developer. Eventually there is a api bad designed or working… but this it is another story

if you use a library with 50.000 lines you can really switch to async function? and then if there is a block code waiting to collect data waiting for a millisecond i dont see where is the problem for a browser view. async and sync might be dicotomy necessary for developement. There is no dogma in computer science but just intelligent solutions related by what you are writing.

For example introduce url:blob://… instead is wrong. In this case is absoutely wrong for a language with garbage collector and for a language used for rendering html. It is a grave error in the design because it can create memory leaks. So it seams w3c uses many times dogma where is not necessary, and dont use it when it is necessary.