【发布时间】:2018-01-03 05:34:45
【问题描述】:
我是 nodejs 的新手,它是回调地狱,我阅读了 node 8 中的 async / await 介绍,并有兴趣以这种方式实现它
我有一组特定的方法,我需要以同步方式依次调用 trello API 例如
- 创建板
- 使用板 id 创建标签
- 使用板 id 创建卡片
- 在卡片上贴标签
- 在卡片中创建列表
- 将每个项目添加到卡片中的列表
你可以想象在 nodejs 中,这需要大量的回调相互嵌套来访问前一个对象
createProjectBoard: function (project) {
t.post("1/board", {
name: project.name,
desc: project.description,
defaultLists: false
}, function (err, board) {
if (err) {
console.log(err);
throw err;
}
//get board id from data
let boardId = board.id
let backlogListId = "";
let highId = "", mediumId = "", lowId = "";
//create labels
t.post("1/labels", {
name: 'High',
color: 'red',
idBoard: boardId
}, function (err, label) {
console.log(err || 'High label created');
if (err) return;
highId = label.id;
});
t.post("1/labels", {
name: 'Medium',
color: 'orange',
idBoard: boardId
}, function (err, label) {
console.log(err || 'Medium label created');
if (err) return;
mediumId = label.id;
});
t.post("1/labels", {
name: 'Low',
color: 'yellow',
idBoard: boardId
}, function (err, label) {
console.log(err || 'Low label created');
if (err) return;
lowId = label.id;
});
//create rest of the lists
t.post("1/lists", { name: "Completed", idBoard: boardId }, function (e, l) {
if (e) {
console.log(e);
return;
}
console.log(l);
t.post("1/lists", { name: "Testing", idBoard: boardId }, function (e, l) {
if (e) {
console.log(e);
return;
}
console.log(l);
t.post("1/lists", { name: "In Progress", idBoard: boardId }, function (e, l) {
if (e) {
console.log(e);
return;
}
console.log(l);
//create backlog list
t.post("1/lists", { name: "Backlog", idBoard: boardId }, function (e, list) {
if (e) {
console.log(e);
return;
}
console.log(list);
backlogListId = list.id;
console.log("backlog card list id:" + backlogListId);
_.each(project.userStories, function (story) {
//assign labels
let labelId = "";
switch (story.complexity.toLowerCase()) {
case 'high':
labelId = highId;
break;
case 'medium':
labelId = mediumId;
break;
default:
labelId = lowId;
}
t.post("1/cards", {
name: story.title,
idLabels: labelId,
idList: backlogListId
}, function (e, card) {
if (e) {
console.log(e);
return;
}
let cardId = card.id;
console.log("created id:" + cardId + ";card:" + story.title);
t.post("1/cards/" + cardId + "/checklists", {
name: "Acceptance Criteria"
}, function (e, checklist) {
if (e) {
console.log(e);
return;
}
console.log('checklist created:');
var clId = checklist.id;
_.each(story.criterion, function (criteria) {
t.post("1/cards/" + cardId + "/checklist/" + clId + "/checkItem", {
name: criteria
}, function (e, checkItem) {
if (e) {
console.log(e);
return;
}
console.log('created check item:' + checkItem);
});
});
});
});
});
});
});
});
});
});
}
我仍然在涉及 __.each 循环的上述代码中遇到问题,它异步调用循环中的所有函数(重新排列它们原本应该是的项目的顺序) - 所以我认为必须有更好的方法来同步调用
我对使用 await / async 清理代码很感兴趣,但是在从异步回调返回对象时遇到了一些麻烦
解决方案基于sails.js,以下是我正在写的TrelloService的节选
考虑以下几点:
createProjectBoard: async function(project) {
//get board id from data
let board;
let boardId = "";
let backlogListId = "";
let highId = "",
mediumId = "",
lowId = "";
try {
await t.post("1/board", {
name: project.name,
desc: project.description,
defaultLists: false
},
function(err, b) {
if (err) {
console.log(err);
throw err;
}
console.log("board" + b);
board = b;
});
//create labels
await t.post("1/labels", {
name: 'High',
color: 'red',
idBoard: board.id
}, function(err, label) {
console.log(err || 'High label created');
if (err) return;
highId = label.id;
});
} catch (err) {
console.log(err);
}
}
我需要在标签请求调用中提供板值,到目前为止我无法检索板对象,尽管我设置了等待关键字
我需要能够从回调函数中获取对象并以同步方式将它们用于后续函数调用
我正在使用 trello api 包装器 node-trello 进行调用 (t)
一种方法是将上述内容包装在更多带有回调的函数中,如下所示,但我认为这不是最佳实践,因为我必须在我需要使用的每个对象上编写包装回调
function foo(url,options,cb){
await t.post(url, options,
function(err, b) {
if (err) {
console.log(err);
throw err;
}
console.log("board" + b);
cb(b);
});
}
var url = "1/board";
var options = {
name: project.name,
desc: project.description,
defaultLists: false
};
foo(url,options,function(board){
console.log(board); //board object
});
欢迎提出任何建议
【问题讨论】:
-
我正在尝试阅读代码,但没有什么意义...如果只有我,并且您的代码在回调中工作,请坚持下去。为了减少回调地狱,您可以简单地重构为更小的函数,并将它们用作回调......
-
我稍后会发布完整的代码,这样更有意义
标签: javascript node.js asynchronous async-await sails.js