0

I have the following code. I'm looking for the the module for promise and $q in Angular 5 but did not find any useful detailed info. The code below is in AngularJS and Javascript.

I want to have to transfer this code to Angular 5 and TypeScript. This is a mock service to mock the data:

angular.module('reports.appActivity').factory('AppActiveMockServices', ['$q', function ($q) { var appActivity; function loadappActivity(params) { var deferred = $q.defer(), cancel = function (reason) { deferred.resolve(reason); }; injectappActivity(params); deferred.resolve(appActivity); return { promise: deferred.promise, cancel: cancel }; } function injectappActivity(params) { var total = Math.floor((Math.random() * 100)), someUsage = total - Math.floor((Math.random() * 80)), tempValue, maxIndex = 0, deltaTime=0, daysSelected, timeBegin, timeEnd, d = new Date(), dayInmilliseconds; appActivity = {total: total, items: []}; deltaTime = 24 * 3600 * 1000; // daily timeBegin = (params.timestamp_start).getTime(); timeEnd =(params.timestamp_end).getTime(); daysSelected = Math.round((timeEnd - timeBegin) / 3600/24/1000); if (daysSelected <= 1 ) { maxIndex = 24 ; // one day , resolution is hourly data deltaTime = 3600 * 1000; // hourly dayInmilliseconds = (d.getTime() - (24 * 3600 * 1000)); } else if (daysSelected <= 7) { maxIndex = 7; // 7 days , resolution is daily data dayInmilliseconds = (d.getTime() - (7* 24 * 3600 * 1000)); } else if (daysSelected <= 30) { maxIndex = 30; // 30 days , resolution is daily data dayInmilliseconds = (d.getTime() - (30* 24 * 3600 * 1000)); } else { // custom range maxIndex = daysSelected; // x days , resolution is daily data dayInmilliseconds = (d.getTime() - (daysSelected * 24 * 3600 * 1000)); } for (var index = 0; index < maxIndex; index++) { d.setTime(dayInmilliseconds + (index * deltaTime)); total = Math.floor((Math.random() * 100)); tempValue = Math.floor((Math.random() * 50)); someUsage = (tempValue > total) ? total : (total - tempValue); var item = { 'total': total, 'some_usage': someUsage, 'date': d.toISOString().slice(0, 19) + 'Z' }; appActivity.items.push(item); } } return { loadappActivity: loadappActivity }; } ]); 

Any help is greatly appreciated.

1
  • The question cannot get a quality answer in current state. It's unclear how this service is supposed to be used, what you expect from the answer and what's your level of expertise with Angular. As a rule of thumb, observables are supposed to be used in Angular. They are more universal than promises, and they are cancellable - while promises aren't. So the service should likely be rewritten to observables - so should other units that consume it. Commented Apr 30, 2018 at 4:34

1 Answer 1

1

looking ... for promise and $q in Angular 5 but not find a useful detail info

In Angular, assuming you left the polyfills.ts alone, you should be able to just use Promise like in my example below

Want to ... transfer this code to using Angular 5 and Typescript.

Here's the basic setup to get you going, i'm not going to copy all of that code over because i see a few things missing/weird in it that might have greater implications to fixing (like for instance you have a params arg that is never used and also a function injectappActivity which is never defined)

consider using the angular cli to generate a service

ng generate service app-active-mock 

and then updating it like so

# app-active-mock.service.ts @Injectable() export class AppActiveMockService { public loadappActivity(): Promise<any> { return Promise.resolve(...) } } 

Also a bit unrelated, but you're likely not coming up with any useful searches for things related to Promises in Angular because for the most part, the community has embraced Observables

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

2 Comments

Thank you for response to my question. Actually this is service to mock the data. Updated the code with the missing function
@Kim2018 I think this answers your question pretty well. What other information are you looking for?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.