Created
July 14, 2020 01:40
-
-
Save Jarrio/e364d0f55e0d2988deadc12acf0b76f2 to your computer and use it in GitHub Desktop.
Cheaty Async in haxe
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
package utilities; | |
import externs.Fetch.FetchOptions; | |
import js.Function; | |
import js.Syntax; | |
import js.lib.Promise; | |
class Async { | |
public static inline function async(f:Void->Void) { | |
return untyped __js__("async ({0})", f)(); | |
} | |
public static inline function asyncAwait<M>(func:Void->Promise<M>) { | |
untyped __js__("async function f(){ | |
console.log('here'); | |
\tvar vtest = await {0}(); | |
\tconsole.dir(vtest); | |
\tconsole.log('Here'); | |
\t} | |
\tf();", func); | |
} | |
public static inline function asyncFetch<M>(url:String, headers:FetchOptions):Promise<M> { | |
return untyped __js__("(async function f(){ | |
\t\tvar get = await fetch({0}, {1}); | |
return get; | |
\t})();", url, headers); | |
} | |
public static inline function asyncFetchJson<M>(url:String, headers:FetchOptions):Promise<M> { | |
return untyped __js__("(async function f(){ | |
\t\tvar get = await fetch({0}, {1}); | |
\t\tvar json = await get.json(); | |
return json; | |
\t})();", url, headers); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment