Comments (4)
Replying to Matias' comment: "We basically want a mechanism where we can supply an array of promises, execute them one at a time serially, and then be able to chain an additional promise to be executed when this set of promises is done."
You should be able to more easily accomplish this with the newly added reduce
operator:
https://github.com/google/promises/blob/master/g3doc/index.md#reduce
Please let us know if you have any additional questions or suggestions on how we can improve our API. Thank you!
from promises.
Hi Matias,
Many thanks for the suggestion! Unfortunately, even if we added something similar to each
, that probably wouldn't work for the case you've mentioned.
The issue with all
is that it expects a sequence of promises, which means some of the promises in such sequence may potentially get resolved concurrently on different queues before they were even passed to all
or before all
had a chance to subscribe for them and observe for state changes.
To guarantee serial resolution of promises you'd need to create them serially. Presumably, something like the following template would work for your scenario:
Swift:
values.reduce(initialPromise) { previousPromise, currentValue in
previousPromise.then { previousValue in
// Do something with the previous and current values to compute the next value.
return nextValue
}
}.then { resultingValue in
// Do something with the resulting value.
}
Objective-C (powered with FBLFunctional):
[[values fbl_reduce:initialPromise
combine:^FBLPromise *(FBLPromise *previousPromise, id currentValue) {
return [previousPromise then:^id(id previousValue) {
// Do something with the previous and current values to compute the next value.
return nextValue;
}];
}] then:^id(id resultingValue) {
// Do something with the resulting value.
return sum;
}];
A naive example:
Swift:
[1, 2, 3].reduce(Promise(0)) { promise, number in
promise.then { partialSum in
partialSum + number
}
}.then { sum in
print(sum)
}
Objective-C:
[[@[@1, @2, @3] fbl_reduce:[FBLPromise resolvedWith:@0]
combine:^FBLPromise *(FBLPromise *promise, NSNumber *number) {
return [promise then:^id(NSNumber *partialSum) {
return @(partialSum.integerValue + number.integerValue);
}];
}] then:^id(NSNumber *sum) {
NSLog(@"%@", sum);
return sum;
}];
Please, let us know if that doesn't work for you or if you have any further ideas or suggestions.
Thanks!
from promises.
I would like to (posthumously?) +1 this issue on the off chance others are doing what I'm doing - currently I use following hack to ensure that parent objects sync before child objects (I don't need to pass data down the chain, but I do care about deterministic order of operations):
RequestFactory(forEntity: Post.self, operations: [.create])
.then { (nothing) -> Promise<Void> in
return RequestFactory(forEntity: Comments.self, operations [.create])
}
It would be lovely to have some sort of syntactic sugar around this, even if it was in an "advanced" section and had a big warning about "here be dragons" or something. I'd be happy to help clarify anything above if needed.
Thanks very much for your work on this library, it is delightful to use.
from promises.
Hi Jeremy, absolutely, any suggestions on such syntactic sugar and usage examples are highly appreciated! We strive to constantly improve this lib.
from promises.
Related Issues (20)
- Contextual type for closure argument list expects 1 argument, which cannot be implicitly ignored
- Objective-C Promises crashes in SwiftUI previews HOT 2
- ios 16 strings convertible to NSError HOT 1
- FBLPromises fails to load building macos flutter app HOT 2
- 'NSInvalidArgumentException', reason: '-[FBLPromise HTTPBody]: unrecognized selector sent to instance HOT 1
- Confused when retry a Promise variable
- type(of: value) is NSError.Type HOT 1
- Bitcode Issue on Xcode 13 when disable it. HOT 1
- Question regarding implementation HOT 3
- Ignore a certain Error and terminate Promise HOT 5
- Please release a new version of promises that includes latest changes for Firebase HOT 4
- Builds including PromisesSwift failing on CI
- Build fails for macOS with Xcode 14.3
- Create releases for new versions HOT 1
- The iOS deployment target 'IPHONEOS_DEPLOYMENT_TARGET' is set to 8.0, but the range of supported deployment target versions is 12.0 to 17.0.99.
- Privacy manifest for sensitive APIs HOT 6
- Version 2.4.0 not compiling HOT 15
- iOS build issue with 2.4.0 HOT 5
- PrivacyInfo.xcprivacy is not included in the framework output built by Carthage HOT 1
- FBLPromises.xcframework missing mac_catalyst support HOT 1
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 promises.