From 0aee6c88cea499f762bd84ab3bce0572d98984c5 Mon Sep 17 00:00:00 2001 From: Ibrahim Hadeed Date: Mon, 18 Jul 2016 10:51:39 -0400 Subject: [PATCH] feat(otherPromise): can work better with plugins that return promises (#304) --- src/plugins/plugin.ts | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/plugins/plugin.ts b/src/plugins/plugin.ts index eed38bac8..0e04679a1 100644 --- a/src/plugins/plugin.ts +++ b/src/plugins/plugin.ts @@ -112,6 +112,16 @@ function wrapPromise(pluginObj: any, methodName: string, args: any[], opts: any return p; } +function wrapOtherPromise(pluginObj: any, methodName: string, args: any[], opts: any= {}) { + return getPromise((resolve, reject) => { + let pluginResult = callCordovaPlugin(pluginObj, methodName, args, opts); + if (pluginResult && pluginResult.error) { + reject(pluginResult.error); + } + pluginResult.then(resolve).catch(reject); + }); +} + function wrapObservable(pluginObj: any, methodName: string, args: any[], opts: any = {}) { return new Observable(observer => { let pluginResult = callCordovaPlugin(pluginObj, methodName, args, opts, observer.next.bind(observer), observer.error.bind(observer)); @@ -199,6 +209,9 @@ export const wrap = function(pluginObj: any, methodName: string, opts: any = {}) else if (opts.eventObservable && opts.event) return wrapEventObservable(opts.event); + else if (opts.otherPromise) + return wrapOtherPromise(pluginObj, methodName, args, opts); + else return wrapPromise(pluginObj, methodName, args, opts); };