Problem/bug 在无服务器上使用 Promise.race()(从之前的调用加载缓存)
Problem/bug with Promise.race() on serverless(loading cache from previous calls)
应用程序在 NodeJs 中是 运行,我们使用 Promise.race() 来使主要承诺超时,如代码所示(这里的“mainPromise”是用于测试的通用承诺) .
const promiseTimeout = new Promise((resolve, _) => {
setTimeout(() => {
resolve('promise Timeout');
}, 20000);
});
const mainPromise = new Promise((resolve, _) => {
setTimeout(() => {
resolve('Main promise finished');
}, 21000);
});
export const triggerVerifySls = async (event: any, context: any) => {
context.callbackWaitsForEmptyEventLoop = false;
const triggerStatus = await Promise.race([mainPromise, promiseTimeout]);
const response = {
statusCode: 200,
body: JSON.stringify(triggerStatus),
};
return response;
};
当我们部署并调用该函数时,它第一次工作,但在其他调用中,Promise.race return 数组中第一个承诺的响应。
输出 Promise.race([mainPromise, promiseTimeout])
$ Serverless reponse: promise Timeout
$ Serverless reponse: Main promise finished
$ Serverless reponse: Main promise finished
$ Serverless reponse: Main promise finished
输出 Promise.race([promiseTimeout, mainPromise])
$ Serverless reponse: promise Timeout
$ Serverless reponse: promise Timeout
$ Serverless reponse: promise Timeout
$ Serverless reponse: promise Timeout
函数似乎缓存了响应,因为它在第一次调用后没有等待 20 秒:
screenshot
- 函数是否缓存响应?
- 功能是否支持Promise.race?
SERVERLESS.YML
frameworkVersion: '2'
provider:
name: aws
runtime: nodejs14.x
memorySize: 2052
timeout: 30
region: us-east-1
functions:
triggerVerifySls:
handler: src/serverless/triggerVerifySls.triggerVerifySls
events:
- http:
path: triggerVerifySls
method: post
是的,就像评论说的那样,让你的 fake promise 函数代替 const promises:
const promiseTimeout = () => new Promise((resolve, _) => {
setTimeout(() => {
resolve('promise Timeout');
}, 20000);
});
const mainPromise = () => new Promise((resolve, _) => {
setTimeout(() => {
resolve('Main promise finished');
}, 21000);
});
然后这样称呼它:
const triggerStatus = await Promise.race([mainPromise(), promiseTimeout()]);
...
应用程序在 NodeJs 中是 运行,我们使用 Promise.race() 来使主要承诺超时,如代码所示(这里的“mainPromise”是用于测试的通用承诺) .
const promiseTimeout = new Promise((resolve, _) => {
setTimeout(() => {
resolve('promise Timeout');
}, 20000);
});
const mainPromise = new Promise((resolve, _) => {
setTimeout(() => {
resolve('Main promise finished');
}, 21000);
});
export const triggerVerifySls = async (event: any, context: any) => {
context.callbackWaitsForEmptyEventLoop = false;
const triggerStatus = await Promise.race([mainPromise, promiseTimeout]);
const response = {
statusCode: 200,
body: JSON.stringify(triggerStatus),
};
return response;
};
当我们部署并调用该函数时,它第一次工作,但在其他调用中,Promise.race return 数组中第一个承诺的响应。
输出 Promise.race([mainPromise, promiseTimeout])
$ Serverless reponse: promise Timeout
$ Serverless reponse: Main promise finished
$ Serverless reponse: Main promise finished
$ Serverless reponse: Main promise finished
输出 Promise.race([promiseTimeout, mainPromise])
$ Serverless reponse: promise Timeout
$ Serverless reponse: promise Timeout
$ Serverless reponse: promise Timeout
$ Serverless reponse: promise Timeout
函数似乎缓存了响应,因为它在第一次调用后没有等待 20 秒: screenshot
- 函数是否缓存响应?
- 功能是否支持Promise.race?
SERVERLESS.YML
frameworkVersion: '2'
provider:
name: aws
runtime: nodejs14.x
memorySize: 2052
timeout: 30
region: us-east-1
functions:
triggerVerifySls:
handler: src/serverless/triggerVerifySls.triggerVerifySls
events:
- http:
path: triggerVerifySls
method: post
是的,就像评论说的那样,让你的 fake promise 函数代替 const promises:
const promiseTimeout = () => new Promise((resolve, _) => {
setTimeout(() => {
resolve('promise Timeout');
}, 20000);
});
const mainPromise = () => new Promise((resolve, _) => {
setTimeout(() => {
resolve('Main promise finished');
}, 21000);
});
然后这样称呼它:
const triggerStatus = await Promise.race([mainPromise(), promiseTimeout()]);
...