You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

48 lines
1.5 KiB

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