S
S
Svyatoslav Khusamov2015-09-24 18:36:42
JavaScript
Svyatoslav Khusamov, 2015-09-24 18:36:42

Promise: How to pass parameter then(param) further down the chain?

Provided that in the first then it is necessary to return Promise. Without it, it is clear how to convey.

...
.then(function(param1) {
  ...
  return Promise;
})
.then(function(param2) {
  // здесь нужно иметь доступ к значению param1 из предыдущего then
})

Answer the question

In order to leave comments, you need to log in

4 answer(s)
N
Nook Scheel, 2015-10-02
@khusamov

.then(function(param1) {
  ...
  return Promise.all([
    Promise.resolve('val1'),
    param1
  ]);
})
.then(function(arr) {
  var param2 = arr[0]
    , param1 = arr[1] 
})

A
Alexey Ukolov, 2015-09-24
@alexey-m-ukolov

Pass it as an argument when resolving a promise:
jsfiddle.net/koceg/b23xgbLy/1

_
_ _, 2015-09-24
@AMar4enko

What about

var param1;
// ...
.then(function(p1) {
    param1 = p1;
    // ...
    return Promise;
})
.then(function(param2) {
  console.log(param1, param2);
})

S
Svyatoslav Khusamov, 2015-09-24
@khusamov

Looks like I found a way:

...
.then(function(param1) {
    ...
    Promise.then(function(param2) {
        return {param1: param1, param2: param2};
    });
    return Promise;
})
.then(function(params) {
    // params.param1, params.param2
})

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question