承诺 运行 顺序不正确
Promises not running in correct order
以下按顺序登录:here1 here2 here5 here3 here4。虽然,here5 应该是最后一个,它根本不会到达 here6。我怎样才能按顺序正确地到达 here1 到 here6 到所有 运行?我正在使用 Sailsjs 和蓝鸟。
//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');
});
};
首先,我不相信 Jobs.create returns 是一个承诺,因此如果您想继续链接,则需要将其包装在一个承诺中。
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);
});
});
};
}
接下来确保 sendResponse 返回的函数也是 returns 一个承诺,因为返回的函数将在链接的 then 块中执行。
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);
});
};
}
以下按顺序登录:here1 here2 here5 here3 here4。虽然,here5 应该是最后一个,它根本不会到达 here6。我怎样才能按顺序正确地到达 here1 到 here6 到所有 运行?我正在使用 Sailsjs 和蓝鸟。
//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');
});
};
首先,我不相信 Jobs.create returns 是一个承诺,因此如果您想继续链接,则需要将其包装在一个承诺中。
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);
});
});
};
}
接下来确保 sendResponse 返回的函数也是 returns 一个承诺,因为返回的函数将在链接的 then 块中执行。
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);
});
};
}