question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

allow returning promise

See original GitHub issue

**I’m submitting a…

[ ] Regression (a behavior that used to work and stopped working in a new release)
[x] Bug report  <!-- Please check the repository for a similar issue or PR before submitting -->
[ ] Support request => <!-- Please check the repository for a similar issue or PR before submitting -->
[x] Feature request
[ ] Documentation issue or request

Current behavior

  1. Using an Angular app (v. 5.2.6), import the following into app.module:
ConfigModule.forRoot({
  deps: [HttpClient],
  provide: ConfigLoader,
  useFactory: configFactory,
}),

where configFactory is:

export function configFactory(http: HttpClient): ConfigLoader {
  return new ConfigHttpLoader(http)
}

Then use getSettings in a service:

@Injectable()
export class MyService {
  constructor(private configService: ConfigService) {
    const url = configService.getSettings("api.url", "")
  }
}

url would always be empty string, when in my config.json file was this:

{
  "api": {
    "url": "www.example.com/api"
  }
}

This is mainly because the HttpLoader is most likely not finished with getting config.json by the time the constructor in my service calls getSettings.

Expected/desired behavior Same code, except have two methods: getSettingsSync and getSettings, where getSettings returns a Promise or Observable when the service is finished with getting the settings.

Then in my code, I would go back to MyService and do this:

constructor(private configService: ConfigService) {
  const url = await configService.getSettings("api.url")
}

What is the motivation / use case for changing the behavior? It is nice to have the getSettings method to always return your expected configuration. Since it didn’t do this, it took me about 2-4 hours to realize why.

Environment

  • Angular version: 5.2.6

  • Browser:

  • Chrome (desktop) version XX
  • Chrome (Android) version XX
  • Chrome (iOS) version XX
  • Firefox version XX
  • Safari (desktop) version XX
  • Safari (iOS) version XX
  • IE version XX
  • Edge version XX

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
FritzHerberscommented, Jun 19, 2018

@fulls1z3 I have the same or a similar problem.

Your HTTP interceptor will/might solve the problem when ConfigService is not yet initialized. It does not solve the problem with a call to GetSettings (within mergeMap) with a resolved ConfigService as the HTTP request initiated by the ConfigHttpLoader has not yet returned data.

I have a service, in the constructor I receive an initialized ConfigService, but the call to GetSettings fails, as no config is yet available. (I think the OP has the same problem).

Is there a possibility to wait for the HTTP request to finish before calling getSettings or have getSettings to wait until the config is available ?

0reactions
unlightcommented, Sep 19, 2019

As a stop gap solution CustomConfigHttpLoader

import { ConfigHttpLoader } from '@ngx-config/http-loader';
import pDefer from 'p-defer';

export class CustomConfigHttpLoader extends ConfigHttpLoader {
    readonly onLoadSettings = pDefer();

    async loadSettings(): Promise<void> {
        const result = await super.loadSettings();
        this.onLoadSettings.resolve();
        return result;
    }
}

in other app initializer

(configService.loader as CustomConfigHttpLoader).onLoadSettings.promise.then(() => {
console.log('loaded');
}));
Read more comments on GitHub >

github_iconTop Results From Across the Web

Promise - JavaScript - MDN Web Docs
Returns a new Promise object that is resolved with the given value. If the value is a thenable (i.e. has a then method),...
Read more >
'return await promise' vs 'return promise' in JavaScript
Is there any difference between using 'return await promise' and 'return promise' in asynchronous JavaScript functions?
Read more >
Javascript: How to access the return value of a Promise object
Let's see how we can access returned data. 1 - .then() chaining. It is the most simple and the most obvious way. fetch( ......
Read more >
Promise callbacks returning promises - javascript
Whenever you are using the keyword return you are passing the result as a parameter to next then 's callback. let p3 =...
Read more >
Async and Await in JavaScript, the extension to a promise.
An async function simply implies that a promise will be returned and if a promise is not returned, JavaScript will automatically wrap it...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found