Created
January 31, 2017 21:26
-
-
Save eriadam/e0a66edc64c75a12c0641552be242e7a to your computer and use it in GitHub Desktop.
iOS-JavaScript sharing data no. 2
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
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