1

I have a feeling that I'm just trying to fit a square peg into a round hole, but I'm trying to apply some things with Angular2 and Typescript, and I'm banging my head against a wall.

I've written a Javascript module that acts as an API client library to an API I'm consuming. It just packages some convenience things like setting up the correct API keys, switching keys based on certain desired data, etc. It's basically just a convenience library.

Most of the methods follow a pattern where you provide a query term and then execute a callback.

So for example:

API.searchAutocomplete("angular", function(err, data) {
    // handle the data/error
});

Inside that method:

searchAutocomplete: function(query, callback) { // set up request with data payload, url, headers, etc

  $.ajax(settings)
    .done(function(response) {
      // callback with success
    })
    .fail(function () {
      // callback with error
    });
}

I'm struggling with trying to understand how to run this function in Typescript in an Angular service with a Promise (square peg round hole). Or should I just pass a callback within the service and treat it like it's Javascript?

My attempt:

public getAutocomplete(query:string): Promise < any > {
    return new Promise((resolve, reject) => {
        API.searchAutocomplete(query, function (err, result) {
            if (err) {
                reject(err);
                return;
            }
            resolve(result);
        });
    });
}

Second, I've been able to load the library into my Angular app but I can't seem to actually make any of the requests. Even if I break in the console and access the library object it doesn't seem to actually make any network requests. Which I really don't understand.

Edit: I've sorted this part out.

When I made my service call return a promise, I had to subscribe to the promise otherwise I wouldn't execute it correctly. I think I still need to understand how to write my service call to return an observable and map the callback response.

3
  • Have you debugged through searchAutocomplete method? can you post the code within this method? Commented Sep 2, 2016 at 4:25
  • If I use the library outside of Angular it works just fine. So code wise, it definitely does it's job. Commented Sep 2, 2016 at 16:01
  • within Angular does the control goes inside the searchAutocomplete function? Commented Sep 2, 2016 at 16:24

1 Answer 1

1

As expected, I was trying to do more work than I should have.

This is pretty simple, just return an observable that calls the 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);
  });
});

}

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.