Skip to content

Instantly share code, notes, and snippets.

@eriadam
Created January 31, 2017 21:26
Show Gist options
  • Save eriadam/e0a66edc64c75a12c0641552be242e7a to your computer and use it in GitHub Desktop.
Save eriadam/e0a66edc64c75a12c0641552be242e7a to your computer and use it in GitHub Desktop.
iOS-JavaScript sharing data no. 2
function fetchDataFromDatabase(query) {
// Calling iOS method here
return new Promise((resolve, reject) => {
// Timeout and reject.
let timeoutId = setTimeout(() => reject(new Error('Timeout')), 3000);
Rx
.Observable
.create(function subscribe(observer) {
// This will be called by the iOS client
didReceiveDataFromDatabase = function(data) {
// Parse, process your data if needed.
observer.next(JSON.parse(data));
}
})
.subscribe(result => {
// Receiving data here, resolving promise
resolve(result);
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment