承诺链接:在接下来的回调中使用先前承诺的结果

osd*_*iab 3 javascript promise ecmascript-harmony ecmascript-6

我正在使用直接的ES6 Promises(使用es6-promise polyfill库),我遇到了一个问题,即访问以前在链接中的承诺的结果.

这个问题在Angular/Q的背景下是相同的,但是我对答案不满意,并想看看是否有更好的方法:

如何访问AngularJS保证链中先前承诺的结果?

请考虑以下代码段:

Student.find().then(function(student) {
        return HelpRequest.findByStudent(student);
    }, function(error) { //... }
).then(function(helpRequest) {
    // do things with helpRequest...
    // PROBLEM: I still want access to student. How can I get access to it?
});
Run Code Online (Sandbox Code Playgroud)

在链式承诺中,我想使用student我在第一个承诺中得到的对象.但正如所写,这无法访问它.我有几个明显的选择:

  1. 将学生存储在外部范围内的变量中(yuck)
  2. 我实际上不知道这是如何工作的,但另一个问题中的解决方案表明我可以调用then结果HelpRequest.findByStudent()和调用Promise.resolve内的组合结果Student.find().then.不过,我认为以下实现不起作用.

    Student.find().then(function(student) {
            var data = {student: student};
            HelpRequest.findByStudent(student).then(function(helpRequest) {
                data.helpRequest = helpRequest;
            });
            // PROBLEM: if HelpRequest.findByStudent(student) is asynchronous, how 
            // does this get the data before returning?
            return data; 
        }, function(error) { //... }
    ).then(function(helpRequest) {
        // do things with helpRequest and student
    });
    
    Run Code Online (Sandbox Code Playgroud)

我绝对不想对方法的helpRequest嵌套内部进行Student.find()处理,因为这违背了链接Promise的目的; 即使第二个例子可以工作到一个可用的状态,它仍然感觉像一个黑客.

有没有更好的方法来实现这一点,而无需引入全局状态或嵌套到我的代码中?例如,有没有办法调用Promise.resolve()多个值,其中一些可能是承诺,而其中一些不是?

我很好奇,希望我有替代方案/能够理解如何在不引入嵌套或状态的情况下正确地完成这项工作!

Ben*_*aum 5

在我看来,承诺的禅就是要弄清楚它们实际上只是异步价值.如果您开始使用它们,这些问题在许多情况下会变得更简单.它不是一颗银弹,但肯定会有所帮助:

在ES5中:

var student = Student.find();
var helpRequest = student.then(HelpRequest.findByStudent);
Promise.all([student, helpRequest]).then(function(results){
    var student = results[0];
    var helpRequest = results[1];
    // access both here
});
Run Code Online (Sandbox Code Playgroud)

在ES6中,具有以下所有功能:

var student = Student.find();
var helpRequest = student.then(HelpRequest.findByStudent);
Promise.all([student, helpRequest]).then(([student, helpRequest]) => {
    // access both here
});
Run Code Online (Sandbox Code Playgroud)

在另一个更丰富的诺言库(蓝鸟):

var student = Student.find();
var helpRequest = student.then(HelpRequest.findByStudent);
Promise.join(student, helpRequest, function(student, helpRequest){
    // access both here
});
Run Code Online (Sandbox Code Playgroud)

  • @OmarDiab一个promise表示value_的_proxy,值已经存在.链接到承诺不会启动任何操作 - 它将发出单个http请求.`find`方法启动请求 - `then`处理程序只允许我们在结束时挂钩它的结果.在`student`上调用`.then`两次与将变量传递给两个不同的函数没有区别 - 它不会执行分配给该变量的代码. (2认同)