javascript - Promise is undefined -
i want change function using promise deal callback
the result of fatch 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 first time , use module "q" change 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 can result "then"
fatch(url).then(console.log(data),console.log(err));
but i'm fail :
typeerror: cannot read property 'then' of undefined
and try 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 didn't work before..
typeerror: promise resolver undefined not function
i changed function , 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 want result :
fatch(url).then(function(data){ console.log(data); },function(err){ console.log(err); });
i don't know why data [] ?
actually , it's first time use promise , i'm confusing , how use promise change function , please me , lot~
your third approach one, you're wrong when "resolve" deferred before returning promise. make resolved synchronously while should make in asynchronous code. solution mix between #1 try , #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; }
Comments
Post a Comment