Skip to content

Commit

Permalink
add async support for the js backend, and some promise helpers (thank…
Browse files Browse the repository at this point in the history
…s to @nschulzke), along with some samples.
  • Loading branch information
TimWhiting committed Sep 29, 2024
1 parent 03c35d7 commit 1973e85
Show file tree
Hide file tree
Showing 9 changed files with 872 additions and 2 deletions.
13 changes: 13 additions & 0 deletions examples/async/file-js.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import fs from 'fs';

function readfile(f) {
return new Promise((resolve, reject) => {
return fs.readFile(f, 'utf8', (err, data) => {
if (err) {
reject(err);
}
resolve(data);
});
});
}

15 changes: 15 additions & 0 deletions examples/async/file-js.kk
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import std/jsextern

extern import
js file "file-js.js"

extern ext/read-file(path: string): io-noexn any
js inline "readfile(#1)"

fun js/read-file(path: string): <io,async> string
Jspromise(read-file(path)).wrap.unsafe-as-string

fun main()
val res = try
js/read-file("examples/async/file-js.kk").println
()
17 changes: 17 additions & 0 deletions examples/async/timer.kk
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import std/async
import std/time/duration
import std/time/timestamp

fun main()
val res = try
val x = ref(0)
timer(1000.milli-seconds) fn(t)
"Hello From Timer".println
x := !x + 1
if !x == 3 then False else True
wait(1.seconds)
"Before timeout".println
wait(10.seconds)
"Hello After Timeout".println
println("Done!")

10 changes: 10 additions & 0 deletions std/async.kk
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*----------------------------------------------------------------------------
Copyright 2024, Koka-Community Authors

Licensed under the MIT License ("The License"). You may not
use this file except in compliance with the License. A copy of the License
can be found in the LICENSE file at the root of this distribution.
----------------------------------------------------------------------------*/

pub import std/async/async
pub import std/async/timer
Loading

0 comments on commit 1973e85

Please sign in to comment.