DEV Community

Discussion on: Thenables: Await objects and cancel or defer your promises!

Collapse
 
einicher profile image
Markus René Einicher • Edited

When I use then to return “this” with its callback it starts an infinite loop of calling then. I don't get why:

class ApiRequest {

    constructor() {

    }

    then(ready) {
        console.log('then start');
        ready(this)
        console.log('then end');
    }

}

let apiRequest = new ApiRequest;
await apiRequest;

console:

then start
then end
then start
then end
then start
then end
then start
then end
then start
then end
then start
then end
then start
then end
then start
then end
then start
then end
...

With .then style it works perfectly:

let apiRequest = new ApiRequest;
apiRequest.then(apiResponse => {
    console.log('apiResponse', apiResponse)
});