Noor Fathima
Noor Fathima

Reputation: 67

Passing promise value to headers

I'm trying to pass the promise value in headers but am not able to do so.

class test{

  constructor(auth_key,auth_secret){
    this.auth_key = auth_key;
    this.auth_secret = auth_secret;
    console.log("============In class test============");
    this.authtoken = this.init().then(function(value){
        return value;
    });
  }

  init(){
    console.log("============In init function============"+this.auth_key);
    let postData = {};
    return this.requestStt('test','POST',postData).then((response) => {
        if (response.status == 200) {
            return response.body.then((response) => {
                //console.log(response.token);
                let apiResp = {stt_token:response.token}
                return apiResp;
             });
        }else {
            console.log(response)
        }
    });
  }

  gettoken(){
    console.log(this.authtoken)
    var reqHeaders = new Headers({
      "Accept":"application/json",
      "Content-Type": "application/json; charset=UTF-8",
      "token":this.authtoken,
      });
  }
}

Getting error because this.authtoken is a promise object.

Can anyone please help me out.

Upvotes: 0

Views: 352

Answers (1)

Jaromanda X
Jaromanda X

Reputation: 1

if you rewrite gettoken like follows:

gettoken(){
    return this.authtoken.then(function(token) {
      return new Headers({
        "Accept":"application/json",
        "Content-Type": "application/json; charset=UTF-8",
        "token":token,
      });
   })
}

then of course, to use those headers, you would need to do something like

xxx.gettoken().then(function(headers) {
    // whatever you do with headers goes here
});

Upvotes: 1

Related Questions