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

时间:2017-07-27 13:02:00

标签: javascript node.js asynchronous async-await sails.js

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

我有一组特定的方法需要以trello API的方式一个接一个地以同步的方式调用 e.g

  1. 创建董事会
  2. 使用board id创建标签
  3. 使用Board id创建卡片
  4. 将标签贴在卡片上
  5. 在卡片中创建列表
  6. 将每个项目添加到卡片列表中
  7. 你可以想象在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);
                                                });
                                            });
                                        });
                                    });
                                });
                            });
                        });
                    });
                });
            });
        }
    

    我仍然遇到上述代码的问题,其中__。涉及每个循环,它异步调用循环中的所有函数(重新安排它们原本应该是的项目的顺序) - 所以我认为必须有一种更好的方式同步进行调用

    我对使用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);
            }
    }
    

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

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

    我正在使用trello api包装器node-trello进行调用(t)

    一种方法是使用回调将上面的函数包含在更多函数中,如下所示,但我认为这不是最佳实践,因为我必须在每个需要使用的对象上编写包装器回调< / p>

    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
    });
    

    任何建议表示赞赏

2 个答案:

答案 0 :(得分:6)

  

我对使用await / async清除代码感兴趣

这有两个步骤:promisification和async。你的代码变得混乱,因为它跳过了第一步。

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

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

第二个步骤是编写async / await逻辑,使用promise-returns函数而不是回调函数。由于他们承诺返回,他们的代码更自然:

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;
}

宣传步骤繁琐且重复。有些库可以自动回调承诺,最值得注意的是Bluebird

答案 1 :(得分:1)

await只能在异步函数中使用,它用于等待Promise解决,而不是你想要的。

要清理代码,请稍微查看Promises,但不要期望它会让您的代码看起来不那么难看,而且您只需更改&#34;回调&# 34;地狱&#34;然后&#34;地狱。

相关问题