forked from react-native-webrtc/react-native-webrtc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
withLegacyCallbacks.js
27 lines (26 loc) · 1.1 KB
/
withLegacyCallbacks.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
module.exports = withLegacyCallbacks;
/**
* Returns a Promise and legacy callbacks compatible function
* @param {Function} method - The function to make Promise and legacy callbacks compatible
* @param {Boolean} [callbackFirst] - Indicate whether or not the success and failure callbacks are the firsts arguments
* @param {Number} [successIndex] - Index of the success callback
* @param {Number} [failureIndex] - Index of the failure callback
* @returns {Function}
*/
function withLegacyCallbacks(method, callbackFirst, successIndex, failureIndex) {
return function(...args) {
const successPos = successIndex || (callbackFirst ? 0 : args.length - 2);
const failurePos = failureIndex || (callbackFirst ? 1 : args.length - 1);
const success = args[successPos];
const failure = args[failurePos];
// If legacy callbacks
if (typeof success === 'function' &&
typeof failure === 'function') {
const newArgs = args.filter(item => item !== success && item !== failure);
return method(...newArgs)
.then(success)
.catch(failure);
}
return method(...args);
};
}