(
values: any,
callback?: {
thenCallback: (value: any) => any;
errorCallback: (err: any) => any;
},
)
| 430 | } |
| 431 | |
| 432 | static allWithCallback<R>( |
| 433 | values: any, |
| 434 | callback?: { |
| 435 | thenCallback: (value: any) => any; |
| 436 | errorCallback: (err: any) => any; |
| 437 | }, |
| 438 | ): Promise<R> { |
| 439 | let resolve: (v: any) => void; |
| 440 | let reject: (v: any) => void; |
| 441 | let promise = new this<R>((res, rej) => { |
| 442 | resolve = res; |
| 443 | reject = rej; |
| 444 | }); |
| 445 | |
| 446 | // Start at 2 to prevent prematurely resolving if .then is called immediately. |
| 447 | let unresolvedCount = 2; |
| 448 | let valueIndex = 0; |
| 449 | |
| 450 | const resolvedValues: any[] = []; |
| 451 | for (let value of values) { |
| 452 | if (!isThenable(value)) { |
| 453 | value = this.resolve(value); |
| 454 | } |
| 455 | |
| 456 | const curValueIndex = valueIndex; |
| 457 | try { |
| 458 | value.then( |
| 459 | (value: any) => { |
| 460 | resolvedValues[curValueIndex] = callback ? callback.thenCallback(value) : value; |
| 461 | unresolvedCount--; |
| 462 | if (unresolvedCount === 0) { |
| 463 | resolve!(resolvedValues); |
| 464 | } |
| 465 | }, |
| 466 | (err: any) => { |
| 467 | if (!callback) { |
| 468 | reject!(err); |
| 469 | } else { |
| 470 | resolvedValues[curValueIndex] = callback.errorCallback(err); |
| 471 | unresolvedCount--; |
| 472 | if (unresolvedCount === 0) { |
| 473 | resolve!(resolvedValues); |
| 474 | } |
| 475 | } |
| 476 | }, |
| 477 | ); |
| 478 | } catch (thenErr) { |
| 479 | reject!(thenErr); |
| 480 | } |
| 481 | |
| 482 | unresolvedCount++; |
| 483 | valueIndex++; |
| 484 | } |
| 485 | |
| 486 | // Make the unresolvedCount zero-based again. |
| 487 | unresolvedCount -= 2; |
| 488 | |
| 489 | if (unresolvedCount === 0) { |
no test coverage detected