【发布时间】:2019-11-06 19:08:30
【问题描述】:
所以我知道这个问题被问了很多,但我试图检索一个在承诺中创建的变量。我在这里看到的示例涉及调用 .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);
});
【问题讨论】:
-
我认为
.filter(task => {会抛出TypeError,除非之前是非标准的Promise?无论如何,你想使用最终解析值的任何地方都应该放在最终的.then -
是的,@CertainPerformance ,体式使用蓝鸟承诺
标签: javascript node.js promise async-await