javascript - Promise chaining: Use result from previous promise in next then callback -


this question has answer here:

i'm using straight es6 promises (with es6-promise polyfill library) , i'm running problem accessing results previous promises in chained ones.

this problem identical in context of angular/q, i'm dissatisfied answer , wanted see if there's better way:

how access result previous promise in angularjs promise chain?

consider code snippet below:

student.find().then(function(student) {         return helprequest.findbystudent(student);     }, function(error) { //... } ).then(function(helprequest) {     // things helprequest...     // problem: still want access student. how can access it? }); 

in chained promise, want use student object got in first promise. written, can't access it. have couple apparent options:

  1. store student in variable in outer scope (yuck)
  2. i don't know how work, solutions in other question suggest can call then on result of helprequest.findbystudent() , promise.resolve combined result inside student.find().then call. below implementation won't work think, though.

    student.find().then(function(student) {         var data = {student: student};         helprequest.findbystudent(student).then(function(helprequest) {             data.helprequest = helprequest;         });         // problem: if helprequest.findbystudent(student) asynchronous, how          // data before returning?         return data;      }, function(error) { //... } ).then(function(helprequest) {     // things helprequest , student }); 

i absolutely don't want processing of helprequest nested inside of student.find() method, defeats purpose of chaining promises; , if second example can worked usable state, still feels hack.

is there better way achieve without having introduce global state or nesting code? instance, there way call promise.resolve() on multiple values, of may promises , of not?

i'm curious, hope have alternatives/can understand how make work without introducing nesting or state!

in opinion, zen of promises figuring out they're asynchronous values. if start using them such these problems become simpler in many cases. it's not silver bullet sure help:

in 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 }); 

in es6, features:

var student = student.find(); var helprequest = student.then(helprequest.findbystudent); promise.all([student, helprequest]).then(([student, helprequest]) => {     // access both here }); 

in richer promise library (bluebird):

var student = student.find(); var helprequest = student.then(helprequest.findbystudent); promise.join(student, helprequest, function(student, helprequest){     // access both here }); 

Comments

Popular posts from this blog

c++ - QTextObjectInterface with Qml TextEdit (QQuickTextEdit) -

javascript - angular ng-required radio button not toggling required off in firefox 33, OK in chrome -

xcode - Swift Playground - Files are not readable -