generated from koka-community/template
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add async support for the js backend, and some promise helpers (thank…
…s to @nschulzke), along with some samples.
- Loading branch information
1 parent
03c35d7
commit 1973e85
Showing
9 changed files
with
872 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!") | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.