Javascript 如何从Node.js中的承诺中获取变量的值

Javascript 如何从Node.js中的承诺中获取变量的值,javascript,promise,Javascript,Promise,我试图弄明白,如何将这个承诺传递给范围之外的变量 var picture = JSON.stringify(this); s.search('danbooru', { tags: ['cat'], limit: 1, random: true }) .then(images => console.log(images[0].common.fileURL)) .catch(err => console.error(err)); 我找不到任何可能的解决办法 如果s.search() c

我试图弄明白,如何将这个承诺传递给范围之外的变量

var picture = JSON.stringify(this);
s.search('danbooru', { tags: ['cat'], limit: 1, random: true })
.then(images => console.log(images[0].common.fileURL))
.catch(err => console.error(err));
我找不到任何可能的解决办法

如果
s.search()

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));
这将有其好处(您可以在以后重用承诺)。但是,这是将承诺分配给变量,而不是承诺的结果:

相反,如果要将搜索结果分配给变量,可以执行以下操作:

let images = null;

s.search('danbooru', { tags: ['cat'], limit: 1, random: true })
.then( img => {
    images = img;
})
.catch( ... )
但是,您需要确保只有在承诺兑现时才能访问
images
变量:

坏:

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.
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(... )
好:

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.
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(... )
然而,声明可变变量并在承诺链中的某个点分配它们可能会导致问题。这将评估重用承诺值的问题,我很确定这将对您的问题有用

如何将此承诺传递给范围外的变量

var picture = JSON.stringify(this);
s.search('danbooru', { tags: ['cat'], limit: 1, random: true })
.then(images => console.log(images[0].common.fileURL))
.catch(err => console.error(err));
不要。使用。超出范围。变量

承诺就是归还东西。归还承诺链。返回要使用的值。在
.then()
回调中使用它们

不要使用全局变量。不要在承诺链之外工作

function search(params) {
    return s.search('danbooru', params)
        .then(images => {
            /* do something with images[0].common.fileURL */
            return images;
        })
        .catch(err => console.error(err));
}

search({ tags: ['cat'], limit: 1, random: true }).then(images => {
    /* do something else with images[0].common.fileURL */
});
你所能做的就是储存和重复使用承诺本身

var result = search({ tags: ['cat'], limit: 1, random: true });

/* ... do some things ... */

result.then(images => { /* do something with images */ });

/* ... do other things ... */

result.then(images => { /* do another thing with images */ });

如果变量是在函数范围之外定义的(例如全局变量),您仍然可以访问它,并且可以像平常一样将结果保存到变量中(在
然后
块中,只需使用
您的变量=图像[0]。common.fileURL
。然后(图像=>VariableInOutScope=图像)
首先,您为什么要返回另一个承诺?如果是关于在某个端点接收承诺,您可以简单地返回原始承诺,不是吗?嗯,有时候,在承诺链中,您需要在另一个承诺调用中重用先前承诺的值。比如,首先抓取一个
用户
,然后抓取一些需要用户的东西,比如说一个
cardId
,然后抓取第三个需要用户和
cardId
:在这种情况下,需要将用户存储在一个变量中。我回答中的代码只是一个示例,在这种情况下,您可以安全地访问
图像
var,而不假装有用。