The following code returns:
output.isPending?: true
output.isRejected?: false
output.isFulfilled?: false
Why? I was expecting output.isRejected
to be true
.
<html>
<head>
<script src="http://cdnjs.cloudflare.com/ajax/libs/q.js/0.9.7/q.js"></script>
<script src="http://jasmine.github.io/2.3/lib/jasmine.js"></script>
</head>
<body>
</body>
<script>
var output, bar;
bar = {
doSomethingAsync: function() {
var d = Q.defer();
d.resolve('result');
return d.promise;
}
};
function Foo(bar) {
this._bar = bar;
this.go = function() {
var deferred = Q.defer();
this._bar.doSomethingAsync()
.then(onSuccess.bind(this, deferred));
return deferred.promise;
}
};
function onSuccess(deferred, result) {
deferred.reject();
}
output = new Foo(bar).go()
.finally(function() {
console.log('output.isPending?:', output.isPending());
console.log('output.isRejected?:', output.isRejected());
console.log('output.isFulfilled?:', output.isFulfilled());
});
</script>
</html>
The promise will always log pending as long as its results are not resolved yet. You must call .then on the promise to capture the results regardless of the promise state (resolved or still pending): let AuthUser = function(data) { return google. login(data. username, data.
Promise resolve() method: Any of the three things can happened: If the value is a promise then promise is returned. If the value has a “then” attached to the promise, then the returned promise will follow that “then” to till the final state. The promise fulfilled with its value will be returned.
Define an async function. Use the await operator to await the promise. Assign the result of using the await operator to a variable.
Consequently, it will always return the final result of every promise and function from the input iterable. Note: The order of the promise array is preserved upon completion of this method.
Because output
is not new Foo(bar).go()
. It is assigned the result of the .finally()
call, and will not be resolved untill the finally
callback is done.
This will work as expected:
var output = new Foo(bar).go();
output.finally(function() {
console.log('output.isPending?:', output.isPending());
console.log('output.isRejected?:', output.isRejected());
console.log('output.isFulfilled?:', output.isFulfilled());
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With