-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Execute serially supporting sync execution.
Refactors executeFieldsSerially to return MaybePromise Fixes #1195
- Loading branch information
Showing
3 changed files
with
84 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/** | ||
* Copyright (c) 2015-present, Facebook, Inc. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @flow | ||
*/ | ||
|
||
/** | ||
* Only returns the value if it acts like a Promise, i.e. has a "then" function, | ||
* otherwise returns void. | ||
*/ | ||
export default function getPromise<T>( | ||
value: Promise<T> | mixed, | ||
): Promise<T> | void { | ||
if ( | ||
typeof value === 'object' && | ||
value !== null && | ||
typeof value.then === 'function' | ||
) { | ||
return (value: any); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/** | ||
* Copyright (c) 2015-present, Facebook, Inc. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @flow | ||
*/ | ||
|
||
import getPromise from './getPromise'; | ||
import type { MaybePromise } from './MaybePromise'; | ||
|
||
/** | ||
* Similar to Array.prototype.reduce(), however the reducing callback may return | ||
* a Promise, in which case reduction will continue after each promise resolves. | ||
* | ||
* If the callback does not return a Promise, then this function will also not | ||
* return a Promise. | ||
*/ | ||
export default function promiseReduce<T, V>( | ||
values: $ReadOnlyArray<V>, | ||
callback: (T, V) => MaybePromise<T>, | ||
initialValue: T, | ||
): MaybePromise<T> { | ||
return values.reduce((accumulator, value) => { | ||
const promise = getPromise(accumulator); | ||
if (promise) { | ||
return promise.then(resolved => callback(resolved, value)); | ||
} | ||
return callback(accumulator, value); | ||
}, initialValue); | ||
} |