Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

An example of a language or of a program? You could try Lua, Python with greenlet, Zig 0.10.0 or Zig master with zigcoro, dozens of libraries that add this sort of capability to C, or becoming a the kind of person who uses search engines when they have questions.

BulletML is not even Turing complete and still has a wait function that does the exact thing mentioned.



>or becoming a the kind of person who uses search engines when they have questions

Just FYI, please become familiar with this site's guidelines before posting [1]; try to make @dang's work a bit easier.

>An example of a language or of a program?

A small code snippet would suffice, any language of your choice that gets the point across more meaningfully. Something like:

  function() {
    // code that shows feature
  }
"... and this is the functionality that you're missing on Javascript".

1: https://news.ycombinator.com/newsguidelines.html


I think there's a reasonable motivating example at https://www.chiark.greenend.org.uk/~sgtatham/coroutines.html, but not a reasonable implementation. A reasonable implementation for C is at https://github.com/creationix/libco and for Zig at https://github.com/rsepassi/zigcoro.

For a real world example, https://github.com/alexnask/iguanaTLS is a TLS library written in terms of the stdlib's reader and writer interfaces. If the read and write methods of the provided reader and writer are normal, non-async functions, then the library's functions are too, and they can be used in a program that does not have an async runtime. If they are async functions, then the library's functions inherit this property, and they can be used in a program that uses async I/O. They could also be used in both modes in the same program, though I can't think of a good reason to do this. Normally in Javascript the consumers of your library would all be imprisoned within an event loop provided by node or the browser, so there would be no point exposing a synchronous variant, but for example see https://nullderef.com/blog/rust-async-sync/ for someone's experience trying to write a library that exposes the same functionality both ways in some other language.


I see your point.

What I would do on JS is ...

... on the lib. side, code a single function that behaves the same when meant to run sync or async.

... on the client side, just await the function every time is called; if the sync version is running you don't return a Promise and await-ing on primitives is free, the program will lock automatically if needed.

Obviously, the trivial solution would be two different methods that do the same thing (as is the case now with things like readFile and readFileSync) but I agree that's not elegant.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: