4 ms·
How can I make a http request
by thescribbblr 5y ago
How can I make a http request
- genezeta 5y agoOnce you local js = require "js" you have direct access to the JS global object, so you can... js.global.fetch(...)
- xialvjun 5y agoSo I got a promise, then how to await it? I don't want to use then callback.
- genezeta 5y agoDon't take my word for it as I only looked into this a while ago but, if I remember correctly, you'll probably need to: a. First build a function in JS wrapping the usage of fetch and the management of the JS Promise there. You'd use lua_yield and lua_resume to yield, and to return back to lua the appropriate value. You would, optimally, do this just once. Or maybe someone has already written such a wrapper so you would simply require it; you'd have to search around for that. b. Once you have that, in the Lua side, you would simply wrap your calling code in a coroutine and you don't need to use any await or then or anything. Something like... coroutine.create(function() local r = fetchWhatever(url, ...); -- the wrapper mentioned above; you just call it print(r) end) But again, don't take my word for it. Things may have changed since I last looked at Fengari.