ReferenceError: resolve is not defined

前端 未结 2 2113
一整个雨季
一整个雨季 2021-02-08 15:08

I have a function to call the google speech api. Looks all is good but I cannot find why its giving me the error. I am beginner with node and promises so not sure why this error

相关标签:
2条回答
  • 2021-02-08 15:19

    Before using resolve you need to pass the resolve as argument:

    In this case,

    return new Promise((resolve, reject) => {
              axios.post('YOUR URL HERE', params)
                    .then(response => {                    
                        resolve() // Here you can use resolve as it passed as argument
                    })
    });
    

    Note: You can use GET, POST for the axios call

    0 讨论(0)
  • 2021-02-08 15:39

    You don't call resolve() from within a .then() handler. There is no such function defined there. If you want to set the resolved value of the promise from within a .then() handler, you can just return that value.

    Change this:

    .then(function(responses) {
          resolve(responses[0]);
          console.log("Result: ", JSON.stringify(responses[0]));
    })
    

    to this:

    .then(function(responses) {
          console.log("Result: ", JSON.stringify(responses[0]));
          return responses[0];
    })
    

    FYI, the resolve() you are likely thinking of is an argument to the new Promise executor callback:

    let p = new Promise(function(resolve, reject) {
        resolve(10);
    });
    

    And, this is the context in which you would use it.

    0 讨论(0)
提交回复
热议问题