0

This is a node.js question.

I am trying to use the contentful API which is a CMS service.

This works:

const contentful = require("contentful");
const client = contentful.createClient({
    space: SPACE_ID,
    accessToken: ACCESS_TOKEN  
})

client.getEntry("27xsh9l8AWraqFQwICeaCn").then((doc)=>{
    console.log(doc);
});

However when I use the exact same code like this:

A.js

module.exports =  class Contentful{

    constructor(){
        this.produceTemplate();  
    }

    async produceTemplate() {
    
        console.log("Fetching Contentful");
                    
        var doc = await client.getEntry("27xsh9l8AWraqFQwICeaCn")
        console.log("done");
    }
}

B.js

class Interpreter {

    constructor(user) {
        let contentful = new Contentful();
        

         // This essentially waits until the contentful object successfully fetches the data and allocates it to a variable called RootTemplate
         if(contentful.RootTemplate == null || contentful.RootTemplate == undefined){
           console.log("Waiting for Contentful service..");
           while(contentful.RootTemplate == null || contentful.RootTemplate == undefined){}
         }
    }
}

When I run the code output is

Fetching Contentful

So the promise

var doc = await client.getEntry("27xsh9l8AWraqFQwICeaCn")

never resolves. It gots stock.

Why is this happening? Is the class blocking me? or is there something wrong with the "contentful" library?

Note: I construct the Interpreter from my index.js, I did not put it because I think it is irrelevant Note2: I create a client object in A.js with exact same space_ID and Access_Token. I cut them out while posting here in order to make code more readeble

isa türk
  • 417
  • 6
  • 17
  • 2
    Is that basically all your program does? Unless the program continues to run after instantiating `Interpreter`, indeed nothing is waiting for the async request to complete. If your program doesn't stay running, it will end before the request completes, and hence output nothing. – deceze Oct 28 '20 at 11:23
  • Does this answer your question? [Async/Await Class Constructor](https://stackoverflow.com/questions/43431550/async-await-class-constructor) – Jared Smith Oct 28 '20 at 11:23
  • @deceze there is a while loop that wait an update from Contentful object. I must have deleted that part when I was simplifying. My mistake. I am updating the code. – isa türk Oct 28 '20 at 12:27
  • @JaredSmith I am not trying to await that function. I am good with that running async. My problem is that fetch data function never returns. I tried the method given in that question anyway. No improvements – isa türk Oct 28 '20 at 12:29
  • A `while` loop is blocking and will not allow any promise to ever be resolved as long as the loop is running. – deceze Oct 28 '20 at 12:30
  • @deceze Thank you, sir. That is indeed the reason. However, I do not understand. How come two async functions can work independently from the main thread flow but they do not resolve when the main thread is jammed. Isn't this the reason why we use async functions. Sorry, I am sort of a beginner – isa türk Oct 28 '20 at 12:44
  • There's only one thread, and only one thing can run on it at a time. `async` is a way to shunt control of that thread back and forth between cooperative tasks. A `while` loop isn't cooperative, it's hogging the thread. You need to completely relinquish the thread, by ending all actively running functions. Then the underlying Javascript event loop will invoke the registered callbacks when an async task completes. – deceze Oct 28 '20 at 13:07
  • Thank you. Appreciate the help – isa türk Oct 28 '20 at 14:05

0 Answers0