Promise is undefined

10,152

Solution 1

Your third approach is the good one, but you're wrong when you "resolve" the deferred just before returning the promise. You make it resolved synchronously while you should make it in your asynchronous code. So the solution is actually a mix between your #1 try and #3 try :

function fatch(url) {

    var deferred = Q.defer();
    superagent.get(url)
        .end(function(err, data) {
            if (err){
                console.log(err);
                deferred.reject(err);
            } 
            var $ = cheerio.load(data.text);
            var result = [];

            $('.question-stream .stream-list__item').each(function(index, ele) {
                var $ele = $(ele);
                var href = path.join(url, $ele.find('.title a').attr('href'));
                var title = $ele.find('.title').text();

                result.push({
                    href: href,
                    title: title
                });
            });

            // console.log(result);
            deferred.resolve(result);

        });

    return deferred.promise;
}

Solution 2

You need to return the promise you create at the end of your two functions.

function fetch(url) {
    var deferred = Q.defer();
    // ...
    return deferred.promise;
}

It seems you are trying to return it from inside the callback. It needs to be returned immediately instead (so that handlers can be added).

Share:
10,152
qianjiahao
Author by

qianjiahao

Updated on June 04, 2022

Comments

  • qianjiahao
    qianjiahao almost 2 years

    i want to change the function for using Promise to deal with the callback

    the result of fatch is like this

    [{ href: 'http:/segmentfault.com/q/1010000002714413',
        title: 'js图片轮播' },
      { href: 'http:/segmentfault.com/q/1010000002714953',
        title: 'stackoverflow, segmentfault 之类的网站是如何实现在输入问题时,下方出现模糊搜索的结果提示?' },
      { href: 'http:/segmentfault.com/q/1010000002711687',
        title: 'js与局域网' } ]
    

    at the first time , i use the module "q" to change the function

    function fatch(url) {
    
        var deferred = Q.defer();
        superagent.get(url)
            .end(function(err, data) {
                if (err){
                    console.log(err);
                    deferred.reject(err);
                } 
                var $ = cheerio.load(data.text);
                var result = [];
    
                $('.question-stream .stream-list__item').each(function(index, ele) {
                    var $ele = $(ele);
                    var href = path.join(url, $ele.find('.title a').attr('href'));
                    var title = $ele.find('.title').text();
    
                    result.push({
                        href: href,
                        title: title
                    });
                });
    
                // console.log(result);
                deferred.resolve(result)
                return deferred.promise;
    
            });
    
    }
    

    i expected that i can get the result by "then"

    fatch(url).then(console.log(data),console.log(err));
    

    but i'm fail :

    TypeError: Cannot read property 'then' of undefined
    

    and i try this way :

    function fatch(url) {
    
    var promise = new Promise();
    superagent.get(url)
        .end(function(err, data) {
            if (err){
                console.log(err);
                promise.reject(err);
            } 
            var $ = cheerio.load(data.text);
            var result = [];
    
            $('.question-stream .stream-list__item').each(function(index, ele) {
                var $ele = $(ele);
                var href = path.join(url, $ele.find('.title a').attr('href'));
                var title = $ele.find('.title').text();
    
                result.push({
                    href: href,
                    title: title
                });
            });
    
            // console.log(result);
            promise.resolve(result)
            return promise;
    
        });
    
    }
    

    but it didn't work as before..

    TypeError: Promise resolver undefined is not a function
    

    i changed the function like this , it's working :

    function fatch(url) {
    
        var deferred = Q.defer();
        var result = [];
        superagent.get(url)
            .end(function(err, data) {
                if (err) {
                    console.log(err);
                }
                var $ = cheerio.load(data.text);
    
    
                $('.question-stream .stream-list__item').each(function(index, ele) {
                    var $ele = $(ele);
                    var href = path.join(url, $ele.find('.title a').attr('href'));
                    var title = $ele.find('.title').text();
    
                    result.push({
                        href: href,
                        title: title
                    });
                });
    
            });
        // console.log(result);
        deferred.resolve(result);
    
        return deferred.promise;
    
    }
    

    and i want to get the result :

    fatch(url).then(function(data){
        console.log(data);
    },function(err){
        console.log(err);
    });
    

    i don't know why the data is [] ?

    actually , it's my first time use Promise and i'm confusing , how to use promise change the function , please help me and thanks a lot~

  • Joel
    Joel almost 9 years
    PS: the deferred is used to transform asynchronized code into promise. So the method must return the promise object, which will be resolved "later", in the asynchronized pieces of code it contains.
  • Saba Ahang
    Saba Ahang about 8 years
    @Joel or @qianjahao can you explain why the first two approaches are incorrect? Is it wrong to pass around a promise to be resolved using then later?
  • Joel
    Joel about 8 years
    The issue was not about the asynchronous chaining using "then", but if you look closely to the first approach proposed, the "return promise" statement was misplaced: it was located in the asynchronous block whereas it should have been at the top level of function "fatch"