if s.search()
returns a Promise, you sure can assign that Promise to a variable:
const searchPromise = s.search('danbooru', { tags: ['cat'], limit: 1, random: true });
searchpromise.then(images => console.log(images[0].common.fileURL))
.catch(err => console.error(err));
That would have its benefits (you can reuse the promise later). However, that is assigning a Promise to a variable, not the result of the promise:
if, instead, you want to assing the search result to a variable, you could do something like:
let images = null;
s.search('danbooru', { tags: ['cat'], limit: 1, random: true })
.then( img => {
images = img;
})
.catch( ... )
However, you need to be sure that you will only access the images
variable when the promise is settled:
bad:
let images = null;
s.search('danbooru', { tags: ['cat'], limit: 1, random: true })
.then( img => {
images = img;
})
.catch( ... )
console.log(images); //bad, images will be null at this point.
fine:
let images = null;
s.search('danbooru', { tags: ['cat'], limit: 1, random: true })
.then(img => {
images = img;
return anotherPromise();
})
.then(someResult => {
console.log(someResult);
console.log(images); //fine, images is guaranteed to be properly set at this point
})
.catch(... )
However, declaring mutable variables and assing them at some point in a promise chain could lead to issues. This really good SO thread assess the problem of reusing promise values and I'm pretty sure it will be useful to your issue.