Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing parameters to a promise chain

Tags:

javascript

I'm having some problems with passing parameters through a promise chain I set up. Here's a basic example of what I'm trying to do.

var dummyReq = { client: null };
var dummyUser = { email: '[email protected]', password: 'admin' };

function printEmail(request, user) {
    return new Promise((resolve, reject) => {
        console.log('Email:', user.email);
        return resolve(request, user);
    });
}

function printPassword(request, user) {
    return new Promise((resolve, reject) => {
        console.log('Password:', user.password); // <-- user is undefined
        return resolve(request, user);
    });
}

printEmail(dummyReq, dummyUser)
    .then(printPassword)
    .catch(function(error) {
    console.log('Unexepected error has occured');
});

In the second promise the parameter user is undefined, how can I pass multiple parameters through a promise chain, as these promises continue more information gets added to the chain, by the end of it I'm passing 5 values through to the last promise.

Should I just use ES6 operators to combine them into an object and deconstruct them on every call? for example return resolve({request, user}) and then const { request, user } = param

like image 891
Hobbyist Avatar asked Jan 24 '17 02:01

Hobbyist


People also ask

Can promises be chained?

Example 2: Chaining the Promise with then() Promise resolved You can call multiple functions this way. In the above program, the then() method is used to chain the functions to the promise. The then() method is called when the promise is resolved successfully. You can chain multiple then() methods with the promise.

What is promise chaining give an example?

Chaining after a catch It's possible to chain after a failure, i.e. a catch , which is useful to accomplish new actions even after an action failed in the chain. Read the following example: new Promise((resolve, reject) => { console. log("Initial"); resolve(); }) .

How do you handle errors in promise chains?

There are two ways in which you can handle errors in your promise chain, either by passing an error handler to then block or using the catch operator.


1 Answers

You are on the right track. Promise objects hold a single fulfilled value which is passed to onfulfilled reaction handlers. A non promise value, or the fulfilled value of a a promise when it eventually becomes fulfilled, is used to fulfill the next promise in a chain of promises. If you want to propagate multiple values to the next promise handler you have to use a single object value to do so.

So let's assume that printEmail and printPassword actually do something asynchronous rather than the synchronous operations in the post.

printEmail becomes:

function printEmail(request, user) {
    return new Promise((resolve, reject) => {
        console.log('Email:', user.email);
        resolve( {request, user} );
    });
}

without a "return" statement inside the executor of the new Promise - the return value of an executor function is not used and will generally be undefined.

printPassword becomes

function printPassword(data) {
    return new Promise((resolve, reject) => {
        console.log('Password:', data.user.password);
        resolve(data);
    });
}}

Whether you deconstruct a data object into variables or not is a stylistic rather than technical question. Similarly whether you pass the same object down to multiple handlers in a chain of promises, deleting or adding properties if required, or create a new object with only the properties needed in following steps is entirely a matter of choice and style.

like image 85
traktor Avatar answered Oct 05 '22 01:10

traktor