tylermcginnis / javascriptvisualizer

A tool for visualizing Execution Context, Hoisting, Closures, and Scopes in JavaScript.

Home Page:https://tylermcginnis.com/javascript-visualizer/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Code finishes at setTimeout, doesn't invoke callback

jacobedawson opened this issue · comments

Hi - thanks for making this tool, it's a lot of fun to play around with and I totally understand it's a WIP / side-project.

I've tested some code out and currently can't work out why it's 'finished' after entering a setTimeout. It finishes at the setTimeout no matter which delay I put in (be it 0, 50 or 1000).

The code definitely works (it's part of a course on Promises, tested in multiple places), and the completion should result in 'done' being printed to the console, but something must be causing the setTimeout to consider itself 'finished' before it invokes the callback! Here's the code if that helps:

function getCurrentCity(callback) {
    setTimeout(function() {
        var city = "New York, NY";
        callback(null, city);
    }, 100);
}
function fetchCurrentCity() {
    var operation = {};
    getCurrentCity(function(error, result) {
        if (error) {
            operation.onError(error);
            return;
        }
        operation.onSuccess(result);
    });
  operation.setCallbacks = function setCallbacks(onSuccess, onError) {
        operation.onSuccess = onSuccess;
        operation.onError = onError;
    };
    return operation;
}
var operation = fetchCurrentCity();
operation.setCallbacks(function (result) {
console.log('done');
}, function(error) {
console.log('error');
});

It seems that any async code doesn't work in the visualizer

console.log("I'm logged!");

setTimeout(function() {
  console.log("I'm not :(");
}, 0);

Uncaught TypeError: Cannot read property 'isGetter' of null is logged in the console.