Last active
February 22, 2017 20:15
-
-
Save matanlurey/a7974eaba2e3219084f8e908f208f4e2 to your computer and use it in GitHub Desktop.
An example of a JavaScript library that fetches your contacts from an "API".
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
@JS() | |
library fetch_contacts_interop; | |
import 'package:func/func.dart'; | |
import 'package:js/js.dart'; | |
// Natively calls "fetch_contacts", but we can use typed Dart code while developing! | |
@JS('fetch_contacts') | |
external void fetchContacts(String prefix, VoidFunc1<List<String>> callback); | |
// Example use | |
void main() { | |
fetchContacts('A', (results) => 'Contacts: $results')); | |
} |
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
// An example of a JavaScript library that fetches your contacts from an "API". | |
// | |
// Example use: | |
// fetch_contacts('C', (contacts) => { ... }); | |
const contacts = [ | |
'Amy', | |
'Beth', | |
'Carl', | |
'Dave' | |
]; | |
function fetch_contacts(prefix, callback) { | |
let results = contacts.filter((contact) => { | |
return contact.startsWith(prefix); | |
}); | |
setTimeout(() => { | |
callback(results); | |
}, 0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment