Skip to content

proposal: create a JS async language plugin to play with top-level-ish await #1841

Open
@hamilton

Description

@hamilton

Working iteratively with async javascript is not always very pretty if you're coming from a data science background. We've built a number of workarounds that help:

1.) fetch chunks make it simple to load data from the web + from the iodide server, which removes the programmatic headaches of working with async js
2.) if your return value of a chunk is a Promise, iodide awaits until the Promise resolves before moving onto the next enqueued chunk

But there's probably more we can do, or at least experiment with. These aren't particularly natural ways of handling data pulling in all situations.

If #1840 lands, we should be able to better parametrize language chunks and try out new ideas. One idea would be to create an async js plugin, which actually just wraps the chunk contents in an async Function, runs it, and then puts whatever is the return value into window[returnVarName] (of which returnVarName is defined as a flag).

This would allow some workflows that are currently difficult boilerplatey with vanilla js chunks:


%% async-js returnVarName="crunchedData"

// even though we use var here, it will be scoped to the chunk only because of how 
// Function works.
var data = await fetchMyData() 

var moreData = await combineTheseDataSources(data, etc.);

moreData // this is the return value added to window.crunchedData
%% js

// plot crunchedData

plot(crunchedData, ...)

I once the flags-in-evaluators issue gets fixed, this might be the cheapest way to use a language plugin to explore this idea. If we like it and are okay with the tradeoffs, and feel that it is valuable enough for Iodide users, we might explore just adding an async flag into iodide js chunks that replicates this behavior.

The tradeoffs primarily are around scoping – var doesn't assign globally as it does elsewhere, nor would something like x = await fetch().... This is a bit of a dealbreaker for casual data science, I think, sine users are still expecting the REPL experience. Unless something speeds up this TC39 proposal I don't know any way around this.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions