所以我知道這個問題經常被問到,但我試圖檢索一個在承諾中創建的變量。我在這里看到的例子涉及調用 .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); });
使用承諾值的異步函數
呼喚遠方
2021-07-12 12:51:10