Comments (6)
I see! I think this can be useful. My only reservation would be inadvertently encouraging people to reach for the Promise library to do common operations like reduce or map, which would incur a lot of unnecessary overhead, rather than using a library like Cryo or similar which doesn't have extra machinery for handling asynchronous code.
That said, I think that when used in the right scenario, an abstraction like this will save time and prevent mistakes.
I imagine it could be implemented over the existing Promise.each
function, something like this:
function Promise.fold(list, predicate, initialValue)
local accumulator = initialValue
return Promise.each(list, function(value)
accumulator = predicate(value, accumulator)
end):andThenReturn(accumulator)
end
I think I prefer the name Promise.fold
, because we accept an initial value rather than use the first element of the list as the default value, and the name fold
is used by other existing Roblox Lua libraries that perform the same function.
If you submit a PR, please:
- Update the CHANGELOG, putting your changes under a # Next header
- Add tests for the function with reasonable cases
- Update the documentation YAML file and add the function (sorry, this is a little tedious!)
Thanks!
from roblox-lua-promise.
This is a better explanation of the feature: http://bluebirdjs.com/docs/api/promise.reduce.html
It is sequential because of the reduce operation, but the advantage is that it returns a promise instead of yielding until the actual result.
from roblox-lua-promise.
Correct me if I'm wrong @oltrep, but this is also has the benefit of a single reject
or resolve
while preserving iteration order (being important for cases when there is a promise -> result -> input to next promise
set of use cases).
Also, this is a great library @evaera, thank you so much! Do you feel an :expect()
is more idiomatic or is inline with your existing implementations?
from roblox-lua-promise.
Also, this is a great library @evaera, thank you so much!
Thank you! 😄
from roblox-lua-promise.
This seems like it might unintentionally create waterfalls (sequential execution instead of concurrent)? Is this the only way that your function could run? Would it be possible to throw a Promise.all somewhere before this?
from roblox-lua-promise.
I feel mixed on this. Assuming you had a reduce function from another library (like cryo or something), could you just throw an :expect() at the end of the Promise to turn it into a yielding call?
from roblox-lua-promise.
Related Issues (20)
- Objects with an "andThen" function are treated as Promises HOT 9
- Promise:finally counts as a consumer, but it probably shouldn't
- Queued callbacks from a cancelled sub-promise should not be run
- Little typo in :expect() documentation
- Use coroutine.close on cancelled promise threads
- Promise.fold does not return correct value if there is an unresolved Promise in the passed list
- Syntax error in documentation example
- Implement Typings For Strict-Typing Luau Users HOT 7
- Unhandled Promise Rejection warning HOT 6
- coroutine.close issue HOT 2
- retryWithDelay signature is wrong
- Stabilize source field
- Promise wally package throws error "Access is denied. (os error 5)" HOT 1
- `Promise.defer` should use `task.defer` HOT 3
- Cannot publish any library HOT 1
- Promise.all should not error for non-promise input values HOT 1
- Document the Error object and all of its members
- Promise.fromEvent as command HOT 1
- The auto-filling doesn't work? HOT 1
- Error in finally handler does not propagate to subsequent catch after a :timeout HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from roblox-lua-promise.