1

I am new to CoffeeScript and am trying to code a promise in a function. I used this reference - Chaining Promises in Coffeescript . But when I code it, I get the error I type .then (response)- TypeError: getter.then is not a function and I get this error if I type .then(respone) - response is not defined . Here is the code for the particular service. Help would be appreciated.

angular.module("deloitte.closeCalendar").factory('labPreferencesService', [ '$location','baseApiService','$routeParams','$translate','labService',( $location, api, $routeParams, $translate, labService) ->
  service = {}
  selectClient = new Object();

  service.setClient = (client) ->
    localStorage.clear();
    delete selectClient[0]; 
    selectClient[0] = client;  
    localStorage.setItem('name', JSON.stringify(selectClient[0]));
    console.log("Name Set!")

  service.getClient = (scope) ->
    if (Object.keys(selectClient).length == 0)
      console.log("Select Client was empty!")
      getter = localStorage.getItem('name')
      getter.then (response) ->display = response;
      console.log (display);
    else
    console.log("Select Client had data !");
    console.log(selectClient);


  return service
])
Community
  • 1
  • 1
Shashank
  • 55
  • 7
  • I am not at all familiar with CoffeeScript, but shouldn't your `getClient` function be returning the promise? – Lex May 31 '16 at 00:25
  • I am just trying to print something to the console at the moment. I will return assign the response to a scope once the promise works :) – Shashank May 31 '16 at 00:28
  • This approach [won't work anyway](http://stackoverflow.com/q/23667086/1048572) if `getter` is a promise. – Bergi May 31 '16 at 00:34
  • Why do you think that [`getItem`](https://developer.mozilla.org/en-US/docs/Web/API/Storage/getItem) would return a promise??? It does not. Don't try to use promises here where you don't need them. – Bergi May 31 '16 at 00:35
  • @Bergi I just want to execute rest of the code once I get a response back a json object back from localStorage.getItem('name'). Is there a different method to do that ? Right now I am just experiencing issues just syntax wise. – Shashank May 31 '16 at 00:40
  • But you're getting the stored string value back immediately as a *return value*, there is no "response" that you'd need to wait for (like with XHR)! And I don't see any CS syntax issues here, you're just doing the wrong method calls. – Bergi May 31 '16 at 00:43
  • @Bergi I agree I will get a string back.The problem is I don't immediately get a string back - it will take time because I am using localStorage. I was getting an empty object in the console one time and a full object the other. I want to make sure that once its full - only then will it continue. – Shashank May 31 '16 at 00:46
  • Yes you *do* get it back immediately from [localStorage](https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage). Please show us what exactly you did when you unexpectedly got back an empty object. – Bergi May 31 '16 at 01:41

0 Answers0