Reputation: 6697
Here is my code:
var gblink = require('./getbloglinks');
new Promise(function (resolve, reject) {
var getLinks = gblink.getBlogLinks("www.example.com");
resolve(getLinks);
}).then(function (data) {
console.log("here");
console.log(data);
return false;
})
gblink.getBlogLinks()
is a function which gets a URL and returns all links in that page (after a short time). When I run my code, immediately console.log("here");
will be printed and then console.log(data);
will be printed as undefined
.
Anyway how can I make that promise waiting until the result of getBlogLinks()
returns?
Noted that when I call gblink.getBlogLinks()
function manually, it works as well, it just takes a while and all I need to do now is implementing a waiting system for that function.
Here is gblink.getBlogLinks()
:
const NN = require('nightmare');
exports.getBlogLinks = function (data){
const n = NN({show:true});
n.goto(data)
.evaluate(() => {
var data = document.querySelectorAll("a[target='_blank']");
arr = [];
i=0;
Array.from(data).forEach( function(x){
arr[i] = x.href;
i++;
});
return arr;
})
.then((data) => {
return n.end(data);
})
}
Upvotes: 0
Views: 55
Reputation: 7250
getBlogLinks
is not returning the promise. Doing that should solve the problem.
const NN = require('nightmare');
exports.getBlogLinks = function (data){
const n = NN({show:true});
return n.goto(data)
.evaluate(() => {
var data = document.querySelectorAll("a[target='_blank']");
arr = [];
i=0;
Array.from(data).forEach( function(x){
arr[i] = x.href;
i++;
});
return arr;
})
.then((data) => {
n.end(data);
return data;
})
};
Edited:
var gblink = require('./getbloglinks');
new Promise(function (resolve, reject) {
var getLinks = gblink.getBlogLinks("www.example.com");
console.log(getLinks);//========= Here You will get Pending promise =========
resolve(getLinks);
}).then(function (data) {
console.log("here");
console.log(data);//========= Here You will get the array=========
return false;
})
Edited2:
var gblink = require('./getbloglinks');
/*
new Promise(function (resolve, reject) {
var getLinks = gblink.getBlogLinks("www.example.com");
console.log(getLinks);//========= Here You will get Pending promise =========
resolve(getLinks);
})*/
//Below is recommended way to chain the promise, avoid promise constructor, if not needed
gblink.getBlogLinks("www.example.com")
.then(function (data) {
console.log("here");
console.log(data);//========= Here You will get the array=========
return false;
})
Upvotes: 2
Reputation: 4537
I am assuming that getBlogLinks()
is a function implemented in your scope of development.
So, what is happening, in your current code of getBlogLInks()
the response is getting returned only when there is a response. But the way you call it, it's synchronous.
Your getBlogLinks
need to be wrapped in a promise.
getBlogLinks(data) {
return new Promise( function(resolve,reject) {
....all your function code
.then(data) {
resolve(data);
}
});
}
then use getBlogLinks().then
and you will have your answer
Upvotes: 0