变量通过堆栈神奇地变成了未定义的



以下是我的代码的相关部分。请注意console.log语句。

async function matchChannel(
context: coda.ExecutionContext,
channelLabel: string
) {
...
console.log(`allTeamIds = ${JSON.stringify(allTeamIds)}`);
try {
// promiseAny is a back-ported shim for Promise.any https://www.npmjs.com/package/promise.any
return await promiseAny(
allTeamIds.map(async (teamId) => {
// Get channels with either id or displayName as channelLabel
console.log(`teamId = ${teamId}`);
const channels = (await listChannels(context, teamId)).body.value;
console.log(`teamId (after) = ${teamId}`);
...
})
);
} catch (error: any) {
...
}
}
export async function listChannels(
context: coda.ExecutionContext,
teamId: string,
ids?: string[],
displayNames?: string[]
) {
console.log(`listChannels(teamId = ${teamId})`);
return await tryDumpFetchParams<ListChannelsResponseBody>(context, {
url: channelsUrl(teamId, ids, displayNames),
method: Method.Get,
headers: jsonApplicationHeader,
});
}
export function channelsUrl(
teamId: string,
ids?: string[],
displayNames?: string[]
) {
function emptyArrayIfUndefined(arr?: Array<any>) {
return arr ?? [];
}
console.log(`channelsUrl(teamId = ${teamId})`);
...
}

我得到以下信息:

allTeamIds = ["ee5e8e7a-ff63-4a6b-89e1-6b28be218ca3","1bd14e82-544f-4a4c-98c5-4c837596e8ae"]
teamId = ee5e8e7a-ff63-4a6b-89e1-6b28be218ca3
listChannels(teamId = ee5e8e7a-ff63-4a6b-89e1-6b28be218ca3)
channelsUrl(teamId = ee5e8e7a-ff63-4a6b-89e1-6b28be218ca3)
teamId = 1bd14e82-544f-4a4c-98c5-4c837596e8ae
listChannels(teamId = 1bd14e82-544f-4a4c-98c5-4c837596e8ae)
teamId (after) = 1bd14e82-544f-4a4c-98c5-4c837596e8ae
channelsUrl(teamId = undefined) <-- WTH
teamId (after) = ee5e8e7a-ff63-4a6b-89e1-6b28be218ca3

为什么teamId神奇地变成了undefined?!这并不是一个被覆盖的闭包。我是疯了还是这种出乎意料的行为?

对不起,这是我的错误。虽然我犯了一个错误,但这是无关的。事实上,我从matchChannel成功返回。

我很确定undefined是由于第一个承诺解决后内存释放。看起来promiseAny在做它的工作。

编辑:

很抱歉之前没有看到评论。我会详细说明。

promiseAny只是要对解决的第一个承诺进行评估。

return await promiseAny(
allTeamIds.map(async (teamId) => {
...
})
);

当成功的promise解析时,其他promise就不再是必要的了,所以节点将开始清理其他promise中涉及的所有内存。

注意,teamId是由allTeamIds.map(...)产生的阵列中的元素。如果我要删除数组中的一个元素,我也会看到未定义的。

const array = [1, 2];
delete array[0];
// prints 'undefined'
console.log(array[0]);

我想这种清理是以一种笨拙的方式进行的,我的print语句在内存清理后执行。当内存已经清理干净时,事件循环可能一直在翻腾。

不幸的是,由于使用自定义沙盒运行时将代码上传到远程服务器,我的复制能力受到了限制。

最新更新