How to create asynchronous code for QuickJS

Basic introduction to the QuickJS asynchronous approach

Since I’ve been reading the QuickJS engine code, I’ve came up with a simple question as an average javascript developer: ¿How the asynchronous code work in a low level?

C Module for running external processes

Our experiment includes a C module for running external processes from scripts compiled with QuickJS, here’s our C external module, it exposes 2 functions: open and close

process.c

JS script for calling external processes

myAsyncScript.js

Compiling this example

Regarding to the compilation, first of all, we should compile QuickJS and run make install for publishing the corresponding headers and static libraries.

Makefile
myAsyncScript.c: myAsyncScript.js
qjsc -flto -e -M process,process -m -o $@ myAsyncScript.js
import { open, close } from "process"
$(OBJDIR)/myAsyncScript.o: myAsyncScript.c
$(CC) $(LDFLAGS) $(CFLAGS_OPT) -c $(INCLUDES) -o $@ myAsyncScript.c

Conclusions

This was a very basic introduction for running asynchronous code by using QuickJS, in spite of the I/O approach isn’t the most performant one, it may be more suitable for embedded environments.

Software Developer ~

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store