-
Notifications
You must be signed in to change notification settings - Fork 24.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reviewed By: svcscm Differential Revision: D2926943 fb-gh-sync-id: 104bcd076f6427aac92e98081e492101ca78de26 shipit-source-id: 104bcd076f6427aac92e98081e492101ca78de26
- Loading branch information
Showing
1 changed file
with
48 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/** | ||
* Copyright (c) 2015-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
* | ||
* Helper for deprecated callback pattern | ||
* | ||
* @providesModule deprecatedCallback | ||
* @flow | ||
*/ | ||
|
||
'use strict'; | ||
|
||
module.exports = function(promise: Promise, callbacks: Array<Function>, type: string, warning: string): Promise { | ||
if (callbacks.length === 0) { | ||
return promise; | ||
} | ||
|
||
let success, error; | ||
|
||
console.warn(warning); | ||
|
||
switch (type) { | ||
case 'success-first': // handles func(success, error), func(success) | ||
[ success, error ] = callbacks; | ||
return promise.then( | ||
res => success(res), | ||
err => error && error(err) | ||
); | ||
case 'error-first': // handles func(error, success) | ||
[ error, success ] = callbacks; | ||
return promise.then( | ||
res => success(res), | ||
err => error(err) | ||
); | ||
case 'node': // handles func(callback) | ||
const [ callback ] = callbacks; | ||
return promise.then( | ||
res => callback(null, res), | ||
err => callback(err) | ||
); | ||
default: | ||
throw new Error(`Type of callbacks not specified. Must be one of 'success-first', 'error-first', or 'node'`); | ||
} | ||
}; |