Git Product home page Git Product logo

Comments (2)

shoumikhin avatar shoumikhin commented on April 28, 2024

Hi @chiragpurohit71085,

I doubt that you really need to use FBLPromiseAwait at all. Normally, you should be good by simply chaining promises into pipelines, possibly with the help of misc extensions operators.

Like for your example I imagine something akin the following:

[[[self getSomeData] then:^id(NSDictionary *notifications) {
  NSMutableArray<FBLPromise *> *modelPromises = [NSMutableArray new];
  for(id key in notifications) {
    NSDictionary *notification = notifications[key];
    NSString *notificationType = notification[@"data"][@"type"];

    if ([notificationType isEqualToString:@"SOMEREFERENCE"] ||
        [notificationType isEqualToString:@"conversation/deleteByInitiatingUser"]) {
      FBLPromise<NSDictionary *> *userDataPromise =
          [[FBLPromise wrapObjectCompletion:^(FBLPromiseObjectCompletion handler) {
            [[[[[[FIRDatabase database] reference] child:USERS_TABLE]
                queryOrderedByChild:@"user_id"]
                    queryEqualToValue:[[obj objectForKey:@"data"] objectForKey:@"user_id"]]
                        observeSingleEventOfType:FIRDataEventTypeValue
                                       withBlock:handler];
          }] then:^id(FIRDataSnapshot *snapshot) {
            return snapshot.value.allObjects.firstObject;   
          }];
      FBLPromise<NSDictionary *> *skillFoundPromise =
          [[FBLPromise wrapObjectCompletion:^(FBLPromiseObjectCompletion handler) {
            [[[[[[FIRDatabase database] reference] child:SOME_TABLE]
                queryOrderedByChild:@"id"]
                    queryEqualToValue:[[obj objectForKey:@"data"] objectForKey:@"skill_id"]]
                        observeSingleEventOfType:FIRDataEventTypeValue
                                       withBlock:handler];
          }] then:^id(FIRDataSnapshot *snapshot) {
            return snapshot.value.allObjects.firstObject;   
          }];
      FNLPromise *modelPromise =
          [[FBLPromise all:@[userDataPromise, skillFoundPromise]] then:^id(NSArray *values) {
            NSDictionary *userData = values.firstObject;
            NSDictionary *skillFound = values.lastObject;
            return [[Model alloc] initWithUserData:userData skillFound:skillFound];
          }];
      [modelPromises addObject:modelPromise];
    }
  }
  return [FBLPromise all:modelPromises];
}] then:^id(NSArray<Model *> *models) {
  [MBProgressHUD hideHUDForView:self.view animated:YES];
  [self do_some_ui_stuff]; 
  return nil;
}];

Which can be simplified and refactored further, if you move some of the patterns into separate methods, for example.

from promises.

chiragpurohit71085 avatar chiragpurohit71085 commented on April 28, 2024

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.