Git Product home page Git Product logo

Comments (4)

 avatar commented on April 28, 2024 1

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.

shoumikhin avatar shoumikhin commented on April 28, 2024

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.

jkmassel avatar jkmassel commented on April 28, 2024

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.

shoumikhin avatar shoumikhin commented on April 28, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.