New Expiremental API for mysql-async only available since build 363 of fivem, this API, will be the official and recommended way in future
https://github.com/brouznouf/fivem-mysql-async/tree/feature/promise-await
- New api using coroutine to have a sync feeling while still being async : local result = MySQL.[execute|fetchScalar|fetchAll|insert](query, params) which is possible due to the
Citizen.Await
function which allow to wait in an asynchronous way promises.
-- Not blocking the main thread, but block current execution context
print(MySQL.fetchScalar('SELECT NOW() as world'))
- All async functions now returns a promise which can be chained by using the next method
-- Promise api
local p = MySQL.Async.fetchScalar("SELECT 1")
p:next(function (result)
print(result)
return "test"
end):next(function (result)
print(result)
error 'An error'
end):next(nil, function(err)
print(err)
return ""
end)
-- Synchronisation
promise.all({
MySQL.Async.execute('SELECT SLEEP(1)'),
MySQL.Async.fetchScalar('SELECT "test"'),
}):next(function (results)
print(json.encode(results))
end)