0

This is my axios Request to call the API.

export function axiosGet (url) {
      return opsutils.get(url)
        .then(function (response) {
            return response.data.data;
        })
        .catch(function (error) {
            return 'An error occured..' + error;
        })
    }

From here i'm calling it asynchrously

async getDirList(data){
      this.domainDir=data.domain_name
      var apiurl="some URL"
      var stat = await axiosGet(apiurl)
      if (status){
        this.domainlog= stat
    }

From here i'm calling the async func defined above

Notify(data){
    var filelist = this.getDirList(data)
    if(filelist){
           var status =  createNotification(data.domain_name,message_stripped,'Degrading web server performance')
}

The ideal should be like this that it should go forward only after the promise is resolved ,right now the var filelist got empty. How do i get to solve this problem ?Thanks in advance

1
  • Don't return a plain string in case of an error Commented Nov 1, 2018 at 19:22

1 Answer 1

4

The problem is this.getDirList(data) is not being accessed asynchronously as well. Remember, because that is async now, it's returning a promise, so you either need to chain it with .then():

Notify(data){
    var filelist = this.getDirList(data)
        .then(data => {
            var status = createNotification(data.domain_name,message_stripped,'Degrading web server performance')
        });

}

Or turn Notify() into an async function as well:

async Notify(data){
    var filelist = await this.getDirList(data);
    if(filelist){
           var status =  createNotification(data.domain_name,message_stripped,'Degrading web server performance')
}

Additionally, make sure you're actually returning data from getDirList so you can utilize the return value when you await it.

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

1 Comment

@Rohit: There's a good tutorial here that I recommend checking out, if you're new to promises/tasks.

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.