| 1 | 
           efrain | 
           1 | 
           /**
  | 
        
        
            | 
            | 
           2 | 
            Copyright 2018 Google Inc. All rights reserved.
  | 
        
        
            | 
            | 
           3 | 
              | 
        
        
            | 
            | 
           4 | 
            Licensed under the Apache License, Version 2.0 (the "License");
  | 
        
        
            | 
            | 
           5 | 
            you may not use this file except in compliance with the License.
  | 
        
        
            | 
            | 
           6 | 
            You may obtain a copy of the License at:
  | 
        
        
            | 
            | 
           7 | 
              | 
        
        
            | 
            | 
           8 | 
            http://www.apache.org/licenses/LICENSE-2.0
  | 
        
        
            | 
            | 
           9 | 
              | 
        
        
            | 
            | 
           10 | 
            Unless required by applicable law or agreed to in writing, software
  | 
        
        
            | 
            | 
           11 | 
            distributed under the License is distributed on an "AS IS" BASIS,
  | 
        
        
            | 
            | 
           12 | 
            WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  | 
        
        
            | 
            | 
           13 | 
            See the License for the specific language governing permissions and
  | 
        
        
            | 
            | 
           14 | 
            limitations under the License.
  | 
        
        
            | 
            | 
           15 | 
            */
  | 
        
        
            | 
            | 
           16 | 
              | 
        
        
            | 
            | 
           17 | 
           #import "FBLPromise+Await.h"
  | 
        
        
            | 
            | 
           18 | 
              | 
        
        
            | 
            | 
           19 | 
           #import "FBLPromisePrivate.h"
  | 
        
        
            | 
            | 
           20 | 
              | 
        
        
            | 
            | 
           21 | 
           id __nullable FBLPromiseAwait(FBLPromise *promise, NSError **outError) {
  | 
        
        
            | 
            | 
           22 | 
             assert(promise);
  | 
        
        
            | 
            | 
           23 | 
              | 
        
        
            | 
            | 
           24 | 
             static dispatch_once_t onceToken;
  | 
        
        
            | 
            | 
           25 | 
             static dispatch_queue_t queue;
  | 
        
        
            | 
            | 
           26 | 
             dispatch_once(&onceToken, ^{
  | 
        
        
            | 
            | 
           27 | 
               queue = dispatch_queue_create("com.google.FBLPromises.Await", DISPATCH_QUEUE_CONCURRENT);
  | 
        
        
            | 
            | 
           28 | 
             });
  | 
        
        
            | 
            | 
           29 | 
             dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  | 
        
        
            | 
            | 
           30 | 
             id __block resolution;
  | 
        
        
            | 
            | 
           31 | 
             NSError __block *blockError;
  | 
        
        
            | 
            | 
           32 | 
             [promise chainOnQueue:queue
  | 
        
        
            | 
            | 
           33 | 
                 chainedFulfill:^id(id value) {
  | 
        
        
            | 
            | 
           34 | 
                   resolution = value;
  | 
        
        
            | 
            | 
           35 | 
                   dispatch_semaphore_signal(semaphore);
  | 
        
        
            | 
            | 
           36 | 
                   return value;
  | 
        
        
            | 
            | 
           37 | 
                 }
  | 
        
        
            | 
            | 
           38 | 
                 chainedReject:^id(NSError *error) {
  | 
        
        
            | 
            | 
           39 | 
                   blockError = error;
  | 
        
        
            | 
            | 
           40 | 
                   dispatch_semaphore_signal(semaphore);
  | 
        
        
            | 
            | 
           41 | 
                   return error;
  | 
        
        
            | 
            | 
           42 | 
                 }];
  | 
        
        
            | 
            | 
           43 | 
             dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
  | 
        
        
            | 
            | 
           44 | 
             if (outError) {
  | 
        
        
            | 
            | 
           45 | 
               *outError = blockError;
  | 
        
        
            | 
            | 
           46 | 
             }
  | 
        
        
            | 
            | 
           47 | 
             return resolution;
  | 
        
        
            | 
            | 
           48 | 
           }
  |