Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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

ReferenceError: resolve is not defined at index.js:57

The problem is in this part of the code:

  return speech
    .longRunningRecognize(responses)
    .then(function(results) {
      var operation = responses[0];
      console.log("Operation: ", operation);
      return operation.promise();
    .then(function(responses) {
      resolve(responses[0]);
      console.log("Result: ", JSON.stringify(responses[0]));

Where the promise

operation.promise() (line 57)

cannot be resolved. It wants to resolve the promise, but it looks like it cannot find the resolve function.

The Google API works like this:

  • First, you do an api call to upload your data and start the process.
  • This gives you back an operation name.
  • This name should be used subsequently to get the result when the result is ready (only takes max 30 sec)
  • I have the feeling it's all working, the call is made, the response comes back. The code waits and then it wants to resolve but it cannot...

    My code is like this (its a cloud function)

      exports.transcribeAudio = functions.storage.object().onChange(event => {
      const object = event.data;
      const filePath = object.name;
      const fileName = filePath.split("/").pop();
      const fileBucket = object.bucket;
      const bucket = gcs.bucket(fileBucket);
      const tempFilePath = path.join(os.tmpdir(), fileName);
      // Exit if this is triggered on a file that is not an image.
      // Get the file name.
      //const fileName = path.basename(filePath);
      console.log(filePath + " name: " + fileName);
      // Exit if the image is already a thumbnail.
      if (!filePath.startsWith("ucl-flac-audio")) {
        console.log("Only flac-audio need to be converted");
        return true;
      // Exit if this is a move or deletion event.
      if (object.resourceState === "not_exists") {
        console.log("This is a deletion event.");
        return true;
      return Promise.resolve()
        .then(() => {
          const audioFilename = "gs://" + fileBucket + "/" + filePath;
          console.log(audioFilename);
          const request = {
            config: {
              encoding: "FLAC",
              languageCode: "fr-FR"
            audio: {
              uri: audioFilename
          return speech
            .longRunningRecognize(request)
            .then(function(responses) {
              var operation = responses[0];
              console.log("Operation: ", operation);
              return operation.promise();
            .then(function(responses) {
              resolve(responses[0]);
              console.log("Result: ", JSON.stringify(responses[0]));
            .catch(function(err) {
              console.error("Failed to get transcript.", err);
              //    reject(err);
        .catch(err => {
          return Promise.reject(err);
                    You're already in a .then callback. Just return responses[0], and the promise created by .then will resolve to that value.
    – Nicholas Tower
                    Oct 9, 2017 at 15:28
                    btw, this: .catch(err => {       return Promise.reject(err);     }); is completely pointless. Just delete these lines.
    – Thomas
                    Oct 9, 2017 at 15:54
    

    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.

    Thank you that explains the problem! Can I then just in that return write it to a google cloud bucket? Something like 'return bucket .file(filename) .save(JSON.stringify(responses[0])) or is that an async action as well and I should use something like return new Promise(resolve,reject) etc – Koen Oct 9, 2017 at 15:53 @Koen - I don't know that Google API. If bucket.file(...).save(...) returns a promise, then you can just return it from within a .then() handler. If not, you need to promisify it first. – jfriend00 Oct 9, 2017 at 16:32

    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

    Exactly , even I ran into same error , I forgot to create new promise for the axios.post , now I understood perfectly , thank you – Hemanth Kollipara Jul 17, 2021 at 7:58

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.