Can I convert callback to promise?
This question already has an answer here:
getDetails('Bob').then(function (details) {
console.log(details);
});
This work only if getDetails return a promise. To create a promise you need $q service.
So getDetails should looks like:
function getDetails(name){
var defered = $q.defer();
defered.resolve('BobDetails');
return defered.promise;
}
Your callback will be called when the promise is resolved, and as param it will get 'BobDetails'
in this example.
下一篇: 我可以将回调转换为承诺吗?