Can I convert callback to promise?

This question already has an answer here:

  • How do I convert an existing callback API to promises? 17 answers

  • 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.

    链接地址: http://www.djcxy.com/p/55530.html

    上一篇: 在Node.js中Promiseifying回调

    下一篇: 我可以将回调转换为承诺吗?