Last active
August 29, 2015 13:56
-
-
Save mrded/8986332 to your computer and use it in GitHub Desktop.
AngularJS: How to resolve promise objects.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function asynkFunction(){ | |
return $timeout(function meAsynk(){ | |
throw new Error('error in meAsynk'); | |
}, 1); | |
} | |
asynkFunction().catch(function (err) { | |
errorHandler(err); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var promise; | |
if (isAsync){ | |
promise = asynkFunction(); | |
} else { | |
var localPromise = $q.defer(); | |
promise = localPromise.promise; | |
localPromise.resolve(42); | |
} | |
promise.then(function (res) { | |
// some code | |
}); | |
// Right way bellow. | |
$q.when(isAsync? asynkFunction(): 42).then(function (res) { | |
// some code | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$q.all([loadSomeInfo(), loadAnotherInfo()]).then(function (results) { | |
doSomethingOnThem(results[0], results[1]); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
current -> | |
deferred = $q.defer() | |
user = $resource('/api/current_user').get({}) | |
user.$promise.then (result) -> | |
deferred.resolve(result) | |
deferred.promise | |
###### | |
current().then (user)-> | |
console.log user |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment