GWorking
GWorking

Reputation: 4341

javascript promise resolve function vs resolve anonymous function

Why is this working (based on the console.log output)

return new Promise(function(resolve) {
    var test = function() {
        console.log('rrrr');
        return $timeout(function(){},100);
    }
    resolve(test());
}

But this is not?

return new Promise(function(resolve) {
    resolve(function() {
        console.log('rrrr');
        return $timeout(function(){},100);
    });
}

Upvotes: 0

Views: 1278

Answers (1)

Erik
Erik

Reputation: 3636

Because the top one calls the test function, but the bottom one only defines the anonymous function.

Try this, it should work:

return new Promise(function(resolve) {
    resolve(function() {
        console.log('rrrr');
        return $timeout(function(){},100);
    }()); // the extra () will call your anonymous function.
}

Upvotes: 2

Related Questions