A partial archive of discourse.wicg.io as of Saturday February 24, 2024.

Sync api or keyword

pinkos
2017-07-07

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
AshleyScirra
2017-07-07

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.

simevidas
2017-07-08

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.

pinkos
2017-07-11

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

tabatkins
2017-07-11

What?

pinkos
2017-07-14

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

pinkos
2017-07-14

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.