diff --git a/lib/init.lua b/lib/init.lua index 35f9a0c..0d844cd 100644 --- a/lib/init.lua +++ b/lib/init.lua @@ -369,6 +369,22 @@ Promise.async = Promise.defer --[=[ Creates an immediately resolved Promise with the given value. + ```lua + -- Example using Promise.resolve to deliver cached values: + function getSomething(name) + if cache[name] then + return Promise.resolve(cache[name]) + else + return Promise.new(function(resolve, reject) + local thing = getTheThing() + cache[name] = thing + + resolve(thing) + end) + end + end + ``` + @param ... any @return Promise<...any> ]=] @@ -386,22 +402,6 @@ end Something needs to consume this rejection (i.e. `:catch()` it), otherwise it will emit an unhandled Promise rejection warning on the next frame. Thus, you should not create and store rejected Promises for later use. Only create them on-demand as needed. ::: - ```lua - -- Example using Promise.resolve to deliver cached values: - function getSomething(name) - if cache[name] then - return Promise.resolve(cache[name]) - else - return Promise.new(function(resolve, reject) - local thing = getTheThing() - cache[name] = thing - - resolve(thing) - end) - end - end - ``` - @param ... any @return Promise<...any> ]=]