The function called in setTimeout will return a promise

Whenever I try to run the code below, I encounter an error message stating that getConfigDetails is not a function. My goal is to have the function getConfigDetails return a promise only if the isConfigLoaded variable is true; otherwise, it should continue calling itself until it is true.


var getConfigDetails = function () {
    if ($rootScope.isconfigloaded) {
        configDetails.roles = $rootScope.orgConfig.roles.slice();
        configDetails.departments = $rootScope.orgConfig.departments.slice();
        configDetails.levels = $rootScope.orgConfig.levels.slice();
        configDetails.designation = $rootScope.orgConfig.designation.slice();
        return Promise.resolve();
     } else {
        setTimeout(function(){
            getConfigDetails();
        },200);
     }
};

getConfigDetails().then(function(){});

Answer №1

One possible solution is as follows:

var retrieveConfigData = new Promise(function(resolve, reject){
  var intervalID = setInterval(function(){
    if ($rootScope.isconfigloaded) {
        //perform additional tasks
        clearInterval(intervalID);
        resolve();
    }
  }, 200);
});

You can utilize it like

retrieveConfigData.then(function(){})

Keep in mind that this is a promise and not a function. If you prefer it to be a function, follow these steps:

function getConfigData() {
  return new Promise(function(resolve, reject){
    var intervalID = setInterval(function(){
      if ($rootScope.isconfigloaded) {
          //perform additional tasks
          clearInterval(intervalID);
          resolve();
      }
    }, 200);
});

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Demonstrating various elements within an application using Vue3

I created two components and attempted to display them in a Vue 3 application. Here is my HTML code: <div id="app"> <image_preview> URL: [[image]] </image_preview> <file_uploader> Counter:[[coun ...

Having trouble showing the JSON data on the HTML page

I have successfully managed to use $http.get in my script to retrieve JSON data, which I receive in Object Form. However, I am facing issues when trying to access the values in the array. https://i.sstatic.net/skKjq.png Below is a snippet of my AppCtrl p ...

Should the article ID be sent to the ajax file, or should the ajax file retrieve the article ID directly? This dilemma arises in a

I am trying to pass the current article ID to an ajax file. The URL of the ajax file is something like www.web.com/plugins/system/ajax.php, so using JRequest::getInt(id) always returns 0 integer. However, in a non-ajax file, I can get the ID the same way. ...

JavaScript has encountered an error due to exceeding the maximum call stack size, resulting in an un

I've been working on a project where I wanted to recreate the retro DVD logo bouncing around the screen but with a custom picture instead. However, I keep encountering an error message that's making it quite frustrating to work through. Here is ...

Adjust the opacity of a MeshBasicMaterial in real-time

<script type="importmap"> { "imports": { "three": "https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="384c504a5d5d784e0816090e0a1608">[email p ...

Storing the file name in a database with Node.js

Seeking assistance with a project idea - Is it feasible to develop an application that can scan a directory for all .txt files, extract the filenames, and store them in a database while ensuring no duplicates are saved? Below is the code snippet I have be ...

What is the best way to conduct a conditional check across all subsequent visible rows of a table?

When a user clicks inside the text input field and presses either the down or up arrow keys, my JavaScript function is triggered. The purpose of this functionality is to search table values and allow the user to select one by pressing the arrow keys. Every ...

Crop images in a canvas using a customized rectangle with the help of JQuery

I am trying to crop an image inside a Canvas element using a selection rectangle. My project utilizes jQuery and I am in search of a plugin that can help me implement this custom selection rectangle on the canvas itself, not just on images. Are there any ...

Unable to locate web element using Selenium in C#

Here is the HTML code I am currently using: <div class="modal-footer"> <button class="btn btn-primary" type="button" value="Show Alert" ng-click="questions.getIrMessage()" data-dismiss="modal">Confirm</button> <button class ...

The uploaded file size exceeds the allowed limit in jquery-file-upload

I'm currently utilizing the jquery-file-upload plugin for file uploads. Within this process, I have a bind to the add callback in order to gather the files for uploading. Upon form submission, I trigger the 'send' command to initiate the upl ...

How can I fill in 3 textboxes with the selected Autocomplete value in MVC 4?

I am trying to implement autocomplete functionality in my form, where a text box is already attached to autocomplete. However, I am unsure how to trigger the ActionResult (which returns JSON) when a value is selected, extract the JSON result, and populate ...

Transforming React Arrays into Objects

I am brand new to React and I'm currently receiving a response from a JSON file that looks like this: methodTypes = ["BOTH","INSIDE","OUTSIDE"] outputTypes = ["STANDARD","DETAILED","ALL_ATTRIBUTES_STANDARD","ALL_ATTRIBUTES_DETAILED"] My goal is to t ...

Having Trouble with the JSX React HTML5 Input Slider

I'm currently utilizing React.JS for a project, where I am creating a range input slider with two options as part of a component. This is the code snippet in question: <input id="typeinp" type="range" min="0" max="5" value="3" step="1"/> Upon ...

How can you efficiently retrieve query parameters in AngularJS?

Looking to extract the URL query parameters using AngularJS? Here's the scenario: I'm working with a URL like this: http://127.0.0.1:8080/test.html?target=bob After checking, location.search gives me "?target=bob". I've searched for ways t ...

Tips for obtaining the correct Javascript output for the number pyramid

I'm currently in the process of creating a half pyramid of numbers, but I'm facing an issue with getting the output to show the total for each line while maintaining everything except the * sign between the numbers. If anyone is able to offer som ...

Display or hide a progress bar in AngularJS when making an AJAX request

Is it possible to display or hide a progress bar during an AJAX service call in AngularJS? I hope this functionality can be applied universally across all service calls. ...

eliminate the common elements between two arrays in typescript/javascript

I have two lists of objects, each containing two fields: let users1 = [{ name: 'barney', uuid: 'uuid3'}, { name: 'barney', uuid: 'uuid1'}, { name: 'barney', uuid: 'uuid2 ...

What distinguishes writing ng-app in the <html> tag compared to the <body> tag?

It is possible to include ng-app in any html element. However, what are the distinctions between including it in <html>, <body>, or <head>. Your assistance would be greatly appreciated. ...

Button activated on second click

As I am working on a project, I encountered an issue with two buttons and one input field. The input field is supposed to take the value of the clicked button, but currently, the function works only after the second click on the button. I have tried using ...

Simple JavaScript File Loading without Rendering or Running Laravel 8 Bootstrap 5

Hey there, I just set up a fresh laravel and bootstrap project. All I've done so far is create a navigation bar. However, when I load the page, I noticed that the app.js file is loading but the CSS is not rendering properly because the app.css stylesh ...