简体   繁体   中英

Passing parameters in an array of functions using a series of promises with bluebird

I have the following functions of Promises:

const func1 = () => new Promise((resolve, reject) => {
    console.log('func1 start');
    setTimeout(() => {
        console.log('func1 complete');
        resolve('Hello');
    }, 1000);
});

const func2 = (param_from_func1) => new Promise((resolve, reject) => {
    console.log('func2 start ' + param_from_func1); // Want to get the value 'Hello' here
    setTimeout(() => {
        console.log('func2 complete');
        resolve('World');
    }, 2000);
});

And to execute those functions in series, I use:

const Promise = require('bluebird');

Promise.series = (promiseArr) => {
  return Promise.reduce(promiseArr, (values, promise) => {
    return promise().then((result) => {
      values.push(result); // And here, I don't know how I can modify this to pass parameters between functions
      return values;
    });
  }, []);
};

And to call:

Promise.series([func1, func2]).then(values => {
    console.log("Promise Resolved. " + values); // Promise Resolved. Hello, World
}, function(reason) {
    console.log("Promise Rejected. " + reason);
});

And everything works fine, because I can have an array of values in series according to the order of the functions.

So, I have two questions:

  1. How can I pass a parameter from func1 to func2 ?

  2. How can I pass a parameter intially to func1 , and from them to func2 ?

If you want to pass on the previous result(s), then just spread the values array to the promise-returning function. Replace:

promise().then

with:

promise(...values).then

 const func1 = () => new Promise((resolve, reject) => { console.log('func1 start'); setTimeout(() => { console.log('func1 complete'); resolve('Hello'); }, 1000); }); const func2 = (param_from_func1) => new Promise((resolve, reject) => { console.log('func2 start ' + param_from_func1); // Want to get the value 'Hello' here setTimeout(() => { console.log('func2 complete'); resolve('World'); }, 2000); }); Promise.series = (promiseArr) => { return Promise.reduce(promiseArr, (values, promise) => { return promise(...values).then((result) => { values.push(result); // And here, I don't know how I can modify this to pass parameters between functions return values; }); }, []); }; Promise.series([func1, func2]).then(values => { console.log("Promise Resolved. " + values); // Promise Resolved. Hello, World }, function(reason) { console.log("Promise Rejected. " + reason); }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/bluebird/3.5.0/bluebird.min.js"></script> 

The [ spread syntax ] replaces the array by separate arguments, as if you had listed them separately. See spread syntax for function calls .

If you want to also pass a value to the first function, then you can specify it in the initial value passed to the reduce method. So then replace:

, []);

with:

, [some_value]);

Note that this value will then also be passed to the second function which will then receive two arguments instead of one. In case you want each function to only receive a single value, namely the one returned by the previous promise, and not all previous promises, then replace

promise(...values)

with

promise(values[values.length-1]). 

That way they all get passed exactly one value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM