I'm using an NPM package called snoowrap, that accesses the Reddit API. It returns results, such as from searches, as Bluebird promises.
I have file reddit.js like this:
const snoowrap = require('snoowrap');
function getFirstSearchResultUrl() {
// Actual Reddit API credentials not included.
const r = new snoowrap({
userAgent,
clientId,
clientSecret,
username,
password,
});
return r
.search({
query: 'query',
subreddit: 'askreddit',
})
.then((posts) => {
return posts[0].url;
});
}
const resultUrl = getFirstSearchResultUrl();
console.log(resultUrl);
But console.log prints [chainable Promise] rather than the URL that is returned.
How can I get it to print the URL that was returned, instead of the promise? I looked through the docs and tried different Bluebird methods like .reflect() and .settle() but they didn't seem to work for me.
Is it possible to prevent the rest of the script from executing until the promise is completed?
getFirstSearchResultreturns a promise so you either chain a.then()stage or if you want to go the imperative way useawait-asyncin an async function. - RedugetFirstSearchResultUrl().then(resultUrl => { console.log(resultUrl); });- Bergithencallback function? - Gary.then()or use the async-await abstraction - Redu