Reputation: 399
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~
Upvotes: 3
Views: 10605
Reputation: 2404
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;
}
Upvotes: 3
Reputation: 262842
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).
Upvotes: 1