Skip to content

Commit

Permalink
Add a deprecatedCallback helper
Browse files Browse the repository at this point in the history
Reviewed By: svcscm

Differential Revision: D2926943

fb-gh-sync-id: 104bcd076f6427aac92e98081e492101ca78de26
shipit-source-id: 104bcd076f6427aac92e98081e492101ca78de26
  • Loading branch information
satya164 authored and facebook-github-bot-4 committed Feb 12, 2016
1 parent 727a2a9 commit 8eddead
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions Libraries/Utilities/deprecatedCallback.js
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'`);
}
};

0 comments on commit 8eddead

Please sign in to comment.