How can I call a function with promise?


How can I call a function with promise?



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.


gblink.getBlogLinks()


console.log("here");


console.log(data);


undefined



Anyway how can I make that promise waiting until the result of getBlogLinks() returns?


getBlogLinks()



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.


gblink.getBlogLinks()



Here is gblink.getBlogLinks():


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);


})
}





You need to call resolve in gblink.getBlogLinks callback
– anttud
Jun 29 at 8:47





is gblink.getBlogLinks() itself a promise ? then you have to implement then() on it.
– codeteq
Jun 29 at 8:47





@codeteq No it is not promise. it is nightmare.
– Martin AJ
Jun 29 at 8:48





can you share the implementation of getbloglinks()?
– Aseem Upadhyay
Jun 29 at 8:49





@AseemUpadhyay Sure ..
– Martin AJ
Jun 29 at 8:49




2 Answers
2



getBlogLinks is not returning the promise. Doing that should solve the problem.


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;
})
};



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;
})





Thank you so much .. it worked
– Martin AJ
Jun 29 at 8:58





@MartinAJ Welcome :)
– dasfdsa
Jun 29 at 8:58





As a note, your solution doesn't work totally. It will work if I write console.log(data) in the .then() block of getBlogLinks function. But still getBlogLinks function doesn't return the result to the promise in the main page.
– Martin AJ
Jun 29 at 9:16


console.log(data)


.then()


getBlogLinks


getBlogLinks





@MartinAJ Can you console.log(getLinks) and check if its a promise of not?
– dasfdsa
Jun 29 at 9:19


console.log(getLinks)





In where should I write console.log(getLinks)? in the resolve block on the promise?
– Martin AJ
Jun 29 at 9:20


console.log(getLinks)


resolve



I am assuming that getBlogLinks() is a function implemented in your scope of development.


getBlogLinks()



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.


getBlogLInks()



Your getBlogLinks need to be wrapped in a promise.


getBlogLinks


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


getBlogLinks().then





Hey Aseem. I think this solution may not work. resolve(data) in your code will not resolve the right data but rather, the data received by getBlogLinks as argument.
– dasfdsa
Jun 29 at 9:07


resolve(data)


data


data


getBlogLinks





oh that might be a misunderstanding let me correct it
– Aseem Upadhyay
Jun 29 at 9:30






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

Opening a url is failing in Swift

Export result set on Dbeaver to CSV