Javascript 链承诺并使用所有变量

Javascript 链承诺并使用所有变量,javascript,promise,Javascript,Promise,我尝试使用Promissions API使用Javascript进行一些获取,然后使用我刚刚获取的所有值。类似于 // Use an Id to find thing 1, use thing 1 Id to find thing2, use thing 2 id to find thing 3, // then use all of them. thing1model.findById(thing1id).then(function(thing1) { return thing2mo

我尝试使用Promissions API使用Javascript进行一些获取,然后使用我刚刚获取的所有值。类似于

// Use an Id to find thing 1, use thing 1 Id to find thing2, use thing 2 id to find thing 3, 
// then use all of them.
thing1model.findById(thing1id).then(function(thing1) {
    return thing2model.findById(thing1.id);
}).then(function(thing2) {
    return thing3model.findById(thing2.id);
}).then(function(thing3) {
    // Here I want to use all of thing1, thing2, thing3...
    someFunction(thing1, thing2, thing3);
}).catch(function(err) {
    console.log(err);
});

问题是
thing1
thing2
在函数调用后超出范围。如何在最后一个
then
函数中使用它们?

您可以将
thing1
thing2
保存在链上方范围内声明的变量中

像这样,

var thing1Data, thing2Data
thing1model.findById(thing1id).then(function(thing1) {
    thing1Data = thing1
    return thing2model.findById(thing1.id);
}).then(function(thing2) {
    thing2Data = thing2
    return thing3model.findById(thing2.id);
}).then(function(thing3) {
    someFunction(thing1Data, thing2Data, thing3);
}).catch(function(err) {
    console.log(err);
});