Henry Boldizsar
Henry Boldizsar

Reputation: 489

Promises not running in correct order

The following is logging in this order: here1 here2 here5 here3 here4. Although, here5 should be last and it's not getting to here6 at all. How can I get here1 to here6 to all run properly in order? I am using Sailsjs and bluebird.

//manually wrapping a callback API like Kaiseki
function createKaisekiObject(className, job) {
    console.log('here2');
    return new Promise(function(resolve, reject) {
        kaiseki.createObject(className, job, function(err, res, body, success) {
            //resolve or reject the promise
            if (err) {
                reject(err);
                return;
            }
            console.log('here3');
            resolve(body);
        });
    });
}

function saveJob(body) {
    console.log('here4', body);

    return Jobs.create({
        owner: req.user.id,
        jobId: body.objectId
    });
}

function sendResponse(req, res) {
    console.log('here5');

    return function(savedJob) {
        console.log('here6', savedJob, req, res);

        // res.json({
        //  success: true,
        //  message: "Running analysis on game",
        //  jobId: savedJob.jobId
        // });
        //return the job
        return savedJob;
    };
}

module.exports.index = function(req, res) {
    console.log('here1');
    var jobId = uuid.v4();
    var job = {
        owner: req.user.id,
        numOfGames: 1,
        numOfGamesCompleted: 0
    };
    var className = 'jobs';
    //promise
    var createJob = createKaisekiObject(className, job)
    .then(saveJob)
    .then(sendResponse(req, res));

    //when first step is done, do more stuff...
    createJob.then(function(savedJob) {
        //do more stuff here...
        console.log('here6');
    });


};

Upvotes: 2

Views: 439

Answers (1)

timotius02
timotius02

Reputation: 54

First of all I don't believe that Jobs.create returns a promise so you need to wrap it in one if you want to continue chaining.

function saveJob(body) {
    console.log('here4', body);

    return function(body) {
        return new Promise(function(resolve, reject) {
            Jobs.create({
                owner: req.user.id,
                jobId: body.objectId
            }).exec(function(err, job) {
                resolve(job);
            });
        });
    };
}

Next make sure that the function that is returned by sendResponse also returns a promise since it is that returned function that will be executed in the chained then block.

function sendResponse(req, res) {
    console.log('here5');

    return function(savedJob) {
        return new Promise(function(resolve, reject) {
            console.log('here6', savedJob, req, res);
            res.json({
                success: true,
                message: "Running analysis on game",
                jobId: savedJob.jobId
            });
            //return the job
            resolve(savedJob);
        });
    };
}

Upvotes: 3

Related Questions