getBlogLinks
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;
})
};
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;
})
编辑2:
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;
})