为了账号安全,请及时绑定邮箱和手机立即绑定

使用承诺值的异步函数

使用承诺值的异步函数

呼唤远方 2021-07-12 12:51:10
所以我知道这个问题经常被问到,但我试图检索一个在承诺中创建的变量。我在这里看到的例子涉及调用 .then 并在那里使用数据,但是我想要做的涉及异步函数——我不能在 .then 块中使用它。这是我的代码。我正在使用 Asana API 调出到期任务列表。它成功地记录了它。但是我想将最后一个块中的列表值保存为我可以在其他地方使用的变量。const asana = require('asana');        const client = asana.Client.create().useAccessToken("xxx");      client.users.me()        .then(user => {            const userId = user.id;            // The user's "default" workspace is the first one in the list, though            // any user can have multiple workspaces so you can't always assume this            // is the one you want to work with.            const workspaceId = user.workspaces[0].id;            return client.tasks.findAll({              assignee: userId,              workspace: workspaceId,              completed_since: 'now',              opt_fields: 'id,name,assignee_status,completed'            });          })          .then(response => {            // There may be more pages of data, we could stream or return a promise            // to request those here - for now, let's just return the first page            // of items.            return response.data;          })          .filter(task => {            return task.assignee_status === 'today' ||              task.assignee_status === 'new';          })          .then(list => {            console.log (util.inspect(list, {              colors: true,              depth: null            }));          })          .catch(e => {            console.log(e);          });
查看完整描述

1 回答

  • 1 回答
  • 0 关注
  • 199 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号