0

Does anyone have an idea how to make a plausible promise chain for this asynchronous JavaScript code:

Updated:

var arr1 = firstFunc(input1, function(err, res){
    if(err) return err;
    return res;
});

var arr2 = firstFunc(input2, function(err, res){
    if(err) return err;
    return res;
});

// When above functions done call this func:

var arr3 = middleFunc(arr1, arr2, function(err, res){
    if(err) return err;
    return res;
});

// When above functions done call this func:

var arr4 = lastFuntion(arr3);
4
  • 1
    That doesn't look asynchronous, as you're getting return values from the functions. It matters, because it makes a big difference to how you structure the promise chain. We can't answer the question as it is, please update it to show actual async code. Commented Nov 27, 2015 at 10:04
  • Can you pass lastFunction and arr3 as arguments to middle function and use them as creating a callback. or you can return the promise from middleFunc, and then check if promise is done then call the lastfn Commented Nov 27, 2015 at 10:13
  • You're looking for Promise.all. Depending on what promise lib you are using, also have a look at Promise.join or the spread method. Commented Nov 27, 2015 at 10:49
  • I updated the code example. I hope I got it right now. Commented Nov 27, 2015 at 14:12

1 Answer 1

1

The functions as they stand are not currently promises. They do, however, follow the async pattern in node.

You can either use something like promisify-node or do it yourself:

// define the first 2 promises by calling firstFunc with inputs
var promise1 = new Promise(function resolver(resolve, reject) {
    firstFunc(input1, function(err, res){
        if(err) reject(err);
        resolve(res);
    });

var promise2 = new Promise(function resolver(resolve, reject) {
    firstFunc(input2, function(err, res){
        if(err) reject(err);
        resolve(res);
    });

// When promise1 & 2 resolve, execute the then handler
Promise.all([promise1, promise2]).then(function (arr) {
    // Grab the resolved values
    var arr1 = arr[0];
    var arr2 = arr[1];

    // return a new promise that is resolved when middleFunc completes
    return new Promise(function resolver(resolve, reject) {
        middleFunc(arr1, arr2, function(err, res){
            if(err) reject(err);
            resolve(res);
        });
    });
}).then(function (arr3) { // Execute this when middleFunc completes
     return lastFuntion(arr3); // This function looks synchronous
}).catch(function (err) {
    // Handle any errors along the way
});

Edit: If you want to create promise1 and promise2 more generically, write a helper function:

// Helper function to construct a promise by calling firstFunc with input
var firstFuncHelper = function (input) {
    return new Promise(function resolver(resolve, reject) {
        firstFunc(input, function(err, res){
            if(err) reject(err);
            resolve(res);
        });
};

var promise1 = firstFuncHelper(input1);
var promise2 = firstFuncHelper(input2);

// Rest of code above remains
Sign up to request clarification or add additional context in comments.

4 Comments

You really should use a generic promisifier instead of repeating the same pattern four times :-)
Agreed, and that is in the answer to use promisify-node :). The OP asked about promises, but failed to demonstrate he was using them, so I figured a more verbose answer using promises would help show the difference between different async patterns (Promise vs. Node Style Callbacks) and how they can be leveraged.
Do I have to repeat "firstFunc" in order to use promises or can it be refactored?
You can construct your promise however you want. I tried to replicate your original code as much as possible so you could follow along. promise1 and promise2 are analogous to your arr1, arr2 declarations. Feel free (and encouraged) to refactor the creation of promise1 and promise2 creations into a helper function that calls firstFunc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.