如何使用异步await从回调函数返回异步函数的值?

Dan*_*ish 6 javascript asynchronous node.js async-await sails.js

我是nodejs的新手,它是回调地狱,我读到了节点8中的async/await简介,并有兴趣以这种方式实现它

我有一组特定的方法需要以trello API的方式一个接一个地以同步方式调用,例如

  1. 创建董事会
  2. 使用board id创建标签
  3. 使用board id创建卡片
  4. 将标签贴在卡上
  5. 在卡片中创建列表
  6. 将每个项目添加到卡片中列出

你可以想象在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);
                                            });
                                        });
                                    });
                                });
                            });
                        });
                    });
                });
            });
        });
    }
Run Code Online (Sandbox Code Playgroud)

我仍然有与上面的代码,其中__问题每个循环受影响,则调用在循环异步所有功能(重新安排项目的顺序它们被认为是原) - 所以我想有必须是一种更好的同步调用方式

我有兴趣使用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);
        }
}
Run Code Online (Sandbox Code Playgroud)

我需要在标签请求调用中提供board值,到目前为止我无法检索board对象,虽然我设置了await关键字

我需要能够从回调函数中获取对象,并以同步方式将它们用于后续函数调用

我正在使用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
});
Run Code Online (Sandbox Code Playgroud)

任何建议表示赞赏

Ste*_*ary 6

我有兴趣使用await/async来清除代码

这有两个步骤:promisification和async.您的代码变得混乱,因为它正在跳过第一步.

Promisification在回调函数周围创建了非常简单的promise-returns包装函数.例如,if tTrello类的实例:

Trello.prototype.postAsync = (url, data) => new Promise((resolve, reject) => {
  this.post(url, data, (err, result) => {
    if (err) { reject(err); }
    else { resolve(result); }
  });
});
Run Code Online (Sandbox Code Playgroud)

所述第二步骤是写async/ await逻辑,使用许返回功能和不回调.由于它们是承诺返回的,因此它们的代码更自然:

const board = await t.postAsync("1/board", {
    name: project.name,
    desc: project.description,
    defaultLists: false
});
console.log("board" + board);

//create labels
let highId;
try {
  highId = await t.postAsync("1/labels", {
      name: 'High',
      color: 'red',
      idBoard: board.id
  });
} catch (err) {
  console.log(err || 'High label created');
  return;
}
Run Code Online (Sandbox Code Playgroud)

宣传步骤繁琐且重复.有些库可以自动回调承诺,最着名的是Bluebird.