i have feeling i'm trying fit square peg round hole, i'm trying apply things angular2 , typescript, , i'm banging head against wall.
i've written javascript module acts api client library api i'm consuming. packages convenience things setting correct api keys, switching keys based on desired data, etc. it's convenience library.
most of methods follow pattern provide query term , execute callback.
so example:
api.searchautocomplete("angular", function(err, data) { // handle data/error });
inside method:
searchautocomplete: function(query, callback) { // set request data payload, url, headers, etc
$.ajax(settings) .done(function(response) { // callback success }) .fail(function () { // callback error }); }
i'm struggling trying understand how run function in typescript in angular service promise (square peg round hole). or should pass callback within service , treat it's javascript?
my attempt:
public getautocomplete(query:string): promise < > { return new promise((resolve, reject) => { api.searchautocomplete(query, function (err, result) { if (err) { reject(err); return; } resolve(result); }); }); }
second, i've been able load library angular app can't seem make of requests. if break in console , access library object doesn't seem make network requests. don't understand.
edit: i've sorted part out.
when made service call return promise, had subscribe promise otherwise wouldn't execute correctly. think still need understand how write service call return observable , map callback response.
as expected, trying more work should have.
this pretty simple, return observable calls external library.
public autocompleteresults(query: string): observable<string[]> { return new observable<string[]>(observer => { api.searchautocomplete(query, function (err, result) { if (err) { console.log(err); observer.next([]); // or observer.error(err); return; } observer.next(result); }); });
}
Comments
Post a Comment