1

I'm using a node machines package (machinepack-wepay) to communicate with Wepay and I'd like to be able to chain it properly.

Take the following example where we will be registering a user, creating an account and sending the email confirm. Along the way we will be storing some of the result info in mongo.

var WePay = require('machinepack-wepay');

// ... extraneous code removed for brevity

var member = req.session.member;

if( !_.has( member, 'wepay' ) ) {

    WePay.userRegister({
        clientId: config.wepay_client_id,
        clientSecret: config.wepay_client_secret,
        email: member.email,
        scope: 'manage_accounts,collect_payments,view_user,send_money',
        firstName: member.firstName,
        lastName: member.lastName,
        originalIp: req.headers['x-forwarded-for'],
        originalDevice: req.headers['user-agent'],
        tosAcceptanceTime: Math.floor(new Date() / 1000),
        callbackUri: config.site_url + '/wepay/user?member=' + member.id,
        useProduction: isProd
    }).exec({
        error: function (err) {
            yourErrorHandler(err);
        },
        success: function (result) {
            Member.update({id: member.id}, {wepay: result}, function (err, updated) {
                if (err) {
                    yourErrorHandler(err);
                }
                else {
                    member = updated[0];
                    WePay.accountCreate({
                        accessToken: member.wepay.access_token,
                        name: 'Account Name',
                        description: 'My new account'
                    }).exec({
                        error: function (err) {
                            yourErrorHandler(err);
                        },
                        success: function (result) {
                            Member.update({id: member.id}, {wepay_account: result}, function (err, updated) {
                                if (err) {
                                    sails.log.error("error updating page:", err);
                                }
                                req.session.member = updated[0];

                                // PATTERN CONTINUES HERE

                            });
                        }
                    });
                }
            });
        }
    });

}
else{
    WePay.userDetails({
        accessToken: member.wepay.access_token,
        useProduction: false,
    }).exec({
        error: function (err){
            yourErrorHandler(err);
        },
        success: function (result){
            _.extend( member.wepay, result );
            Member.update({id: req.session.current_page.id}, member, function (err, updated) {
                if (err) {
                    sails.log.error("error updating page:", err);
                }
                req.session.member = updated[0];

                // PATTERN CONTINUES HERE

            });
        },
    });
}
Tomas
  • 3,054
  • 5
  • 27
  • 39
  • 1
    [Promisify](http://stackoverflow.com/questions/22519784/how-do-i-convert-an-existing-callback-api-to-promises?rq=1) those `exec` and `update` methods. – Bergi Jun 02 '16 at 16:32
  • Sorry, but that's not an answer or all that helpful to the question really. This code has an if/else statement and nested callbacks that could be written in a Promised way that I'm not sure of because I'm not all that familiar with them yet. – Tomas Jun 03 '16 at 13:34
  • Still, promisifying those is the first step. Can you do that? If so, try to [continue with these rules of thumb](http://stackoverflow.com/a/25756564/1048572). `if/else` inside promise callbacks is nothing special (see [example](http://stackoverflow.com/a/21913501/1048572)), just don't forget to `return` what you need. Once you get those going, start [unnesting the callbacks](http://stackoverflow.com/a/22000931/1048572). You see, I've essentially written a complete promise tutorial already and am not keen on repeating everything here :-) If you need help with specific things, feel free to ask. – Bergi Jun 03 '16 at 13:42

0 Answers0