Skip to content

Embrace JavaScript async/await/promises #70

@FunctionPoint

Description

@FunctionPoint

Okay then,

-- Preface
So JavaScript now has a very, very unforunate way of implementing asyncrounous execution,
where the callee determines if a function should be run async, in stead of the caller.

To reduce boilerplate code for sequencing calls there is the 'await' keyword
that enables sequential synchrounous calling of async functions,
but with the restriction that the function containing 'await' must itself be async.

This causes JavaScript's 'async desease' where a whole tree of functions needs to become async,
of there is one async library call in the bottom, say fetching a single row from a SQL table.
That's why SmallJS has resisted spreading async promises upto now,
by immediately following an 'async' JS function call with a 'then:' callback block, that may be sync.

-- Direction
But more and more libraries are using async execution with promises,
also for sync operations that only take a few milliseconds to execute.
Writing the 'then' & 'catch' clauses to flatten them becomes too much boilerplate code,
and forces splitting up of functions that just need to just execute sequentially.
E.g.: A database read, update and delete transaction over multiple SQL tables.

-- Change (the real issue)
So now SmallJS will migrate its libraties to async-await, with async functions returning JS promises.
When using 'await' this will read very nicely, sequentially.
The drawback is that calling an async function in JS always returns a JS promise,
and there is no way of converting it to ST promise first.
So if you don't 'await' the JS Promise, you will have to convert it to a ST Promise first,
using: 'Promise fromJs: '

I think the trade-off is worth it, with the greatest benefits for code processing files, databases and encryption.
(And code that does not need 'async' is not affected)

-- Consequences
This will be a very breaking change, because most '...then:' functions will be converted to 'async' functions.
Also all calling code will have to ma changed too, using 'await' or 'Promise fromJs: ... then:'.
Because of these major changes, the next version of SmallJS that implements this, will have version 2.0.

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions