Skip to content

remove es6-promisify and better wrapApi function #38

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 14 additions & 20 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,30 @@
*/

import {NativeModules, NativeAppEventEmitter} from 'react-native';
import promisify from 'es6-promisify';

const {WeiboAPI} = NativeModules;

// Used only with promisify. Transform callback to promise result.
function translateError(err, result) {
if (!err) {
return this.resolve(result);
}
if (typeof err === 'object') {
if (err instanceof Error) {
return this.reject(ret);
}
return this.reject(Object.assign(new Error(err.message), { errCode: err.errCode }));
} else if (typeof err === 'string') {
return this.reject(new Error(err));
}
this.reject(Object.assign(new Error(), { origin: err }));
}

function wrapApi(nativeFunc) {
if (!nativeFunc) {
return undefined;
}
const promisified = promisify(nativeFunc, translateError);
return (...args) => {
return promisified(...args);
return new Promise((resolve, reject) => {
nativeFunc.apply(null, [
...args,
(error, result) => {
if (!error) {
return resolve(result);
}
if (typeof error === 'string') {
return reject(new Error(error));
}
reject(error);
},
]);
});
};
}

// Save callback and wait for future event.
let savedCallback = undefined;
function waitForResponse(type) {
Expand Down
7 changes: 2 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-native-weibo",
"version": "3.0.4",
"version": "3.0.5",
"description": "新浪微博登录、分享模块",
"main": "index.js",
"scripts": {
Expand All @@ -24,8 +24,5 @@
"bugs": {
"url": "https://github.com/reactnativecn/react-native-weibo/issues"
},
"homepage": "https://github.com/reactnativecn/react-native-weibo#readme",
"dependencies": {
"es6-promisify": "^3.0.0"
}
"homepage": "https://github.com/reactnativecn/react-native-weibo#readme"
}