{"id":411394,"date":"2024-06-29T21:56:34","date_gmt":"2024-06-29T21:56:34","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=411394"},"modified":"-0001-11-30T00:00:00","modified_gmt":"2024-06-29T21:56:34","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=411394","title":{"rendered":"<span>\u0411\u043e\u0440\u044c\u0431\u0430 \u0441 \u0433\u043e\u043d\u043a\u0430\u043c\u0438 (race conditions) \u0432 JavaScript \u043d\u0430 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u043a\u0435\u0448\u0435\u043c<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0443\u044e \u0437\u0430\u0434\u0430\u0447\u0443. \u041d\u0430\u043c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0437\u043e\u0432\u044b \u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0433\u043e API, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0441\u0447\u0438\u0442\u0430\u044e\u0442\u0441\u044f \u0434\u043e\u0440\u043e\u0433\u0438\u043c\u0438, \u0438, \u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e, \u0438\u0445 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043a\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432 Redis. \u041c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u0441\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0439 NodeJS (\u0432\u0435\u0440\u0441\u0438\u0438 14+), \u0430 \u0437\u043d\u0430\u0447\u0438\u0442 \u0438 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u0438 async \/ await.<\/p>\n<p>\u041d\u0430\u043f\u0438\u0448\u0435\u043c \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u043a\u043b\u0430\u0441\u0441 \u043e\u0431\u0435\u0440\u0442\u043a\u0443 \u043d\u0430\u0434 \u0432\u044b\u0437\u043e\u0432\u043e\u043c API, \u0433\u0434\u0435 \u0441\u0430\u043c \u0432\u044b\u0437\u043e\u0432 \u0431\u0443\u0434\u0435\u043c \u044d\u043c\u0443\u043b\u0438\u0440\u043e\u0432\u0430\u0442\u044c 2-\u0441\u0435\u043a\u0443\u043d\u0434\u043d\u044b\u043c \u0442\u0430\u0439\u043c\u0430\u0443\u0442\u043e\u043c.<\/p>\n<pre><code class=\"javascript\">class ApiWrapper {     #callTimes = 0;      async apiCall(payload) {         return new Promise(resolve => {             setTimeout(() => {                 this.#callTimes++;                 resolve(`success: ${payload}`)             }, 2000);         })     }      get callTimes() {         return this.#callTimes;     } }  const run = async () => {     const api = new ApiWrapper();      \/\/ \u044d\u043c\u0443\u043b\u0438\u0440\u0443\u0435\u043c \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u044b\u0439 \u0432\u044b\u0437\u043e\u0432 API 4 \u0440\u0430\u0437\u0430     const prDirect = await Promise.all([api.apiCall('test'), api.apiCall('test'), api.apiCall('test'), api.apiCall('test')]);     console.log(prDirect); \/\/ => ['success: test', 'success: test', 'success: test', 'success: test']     console.log(apiCache.callTimes); \/\/ => 4 }  run();<\/code><\/pre>\n<p>\u042f \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u044c\u043d\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u043b \u0432 \u043a\u043b\u0430\u0441\u0441 \u0441\u0447\u0435\u0442\u0447\u0438\u043a \u0432\u044b\u0437\u043e\u0432\u043e\u0432 callTimes &#8212; \u043e\u043d \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0440\u0430\u0437 \u043c\u044b \u0432\u044b\u0437\u0432\u0430\u043b\u0438 \u043c\u0435\u0442\u043e\u0434 API. \u0412 \u0434\u0430\u043d\u043d\u043e\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u0443 \u043d\u0430\u0441 4 \u043f\u0440\u044f\u043c\u044b\u0445 \u0432\u044b\u0437\u043e\u0432\u0430.<\/p>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u043a \u043a\u043e\u0434\u0443 \u043a\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0432 Redis. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043f\u0430\u043a\u0435\u0442 redis@next.<\/p>\n<details class=\"spoiler\">\n<summary>\u041a\u043e\u0434 \u0441 \u043a\u0435\u0448\u0438\u0440\u0443\u044e\u0449\u0438\u043c \u043c\u0435\u0442\u043e\u0434\u043e\u043c cachedApiCall<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"javascript\">class ApiWrapper {     #client;     #callTimes = 0;      constructor(url) {         \/\/ \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u043a\u043b\u0438\u0435\u043d\u0442\u0430 Redis         this.#client = createClient({             url         });     }      async init() {         \/\/ \u041f\u043e\u0434\u043a\u043b\u044e\u0447\u0430\u0435\u043c\u0441\u044f \u043a Redis         await this.#client.connect();     }      async apiCall(payload) {         return new Promise(resolve => {             setTimeout(() => {                 this.#callTimes++;                 resolve(`success: ${payload}`)             }, 2000);         })     }      async cachedApiCall(payload) {         let data = await this.#client.get(payload);         if (data === null) {             \/\/ cache for 5 minutes             data = await this.apiCall(payload);             await this.#client.set(payload, data, {                 EX: 60 * 5             });         }         return data;     }      get callTimes() {         return this.#callTimes;     } }  const run = async () => {     const api = new ApiWrapper('redis:\/\/10.250.200.9:6379\/6');     await api.init();      const prCached = await Promise.all([api.cachedApiCall('test'), api.cachedApiCall('test'), api.cachedApiCall('test'), api.cachedApiCall('test')]);     console.log(prCached); \/\/ => ['success: test', 'success: test', 'success: test', 'success: test']     console.log(api.callTimes); \/\/ => 4 }<\/code><\/pre>\n<\/div>\n<\/details>\n<p>\u041d\u0435\u0441\u043c\u043e\u0442\u0440\u044f \u043d\u0430 \u0442\u043e, \u0447\u0442\u043e \u043c\u044b \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u043c cachedApiCall, \u043d\u0430\u0448 \u0441\u0447\u0435\u0442\u0447\u0438\u043a \u0432\u044b\u0437\u043e\u0432\u043e\u0432 API \u0432\u0441\u0451 \u0440\u0430\u0432\u043d\u043e \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u0446\u0438\u0444\u0440\u0443 4. \u042d\u0442\u043e \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u0438\u0437-\u0437\u0430 \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e\u0441\u0442\u0435\u0439 \u0440\u0430\u0431\u043e\u0442\u044b async \/ await.<\/p>\n<p>\u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0434\u0435\u0442\u0430\u043b\u044c\u043d\u0435\u0439 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043a\u0435\u0448\u0438\u0440\u0443\u044e\u0449\u0438\u0439 \u043c\u0435\u0442\u043e\u0434. \u042f \u0435\u0433\u043e \u043d\u0430\u043f\u0438\u0441\u0430\u043b \u0442\u0430\u043a, \u043a\u0430\u043a \u0435\u0441\u043b\u0438 \u0431\u044b \u044f \u0440\u0430\u0431\u043e\u0442\u0430\u043b \u0441 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u043c \u043a\u043e\u0434\u043e\u043c.<\/p>\n<pre><code class=\"javascript\">    async cachedApiCall(payload) {         \/\/ \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0434\u0430\u043d\u043d\u044b\u0435 \u0438\u0437 \u043a\u0435\u0448\u0430         let data = await this.#client.get(payload);         \/\/ \u0435\u0441\u043b\u0438 \u0434\u0430\u043d\u043d\u044b\u0445 \u043d\u0435\u0442, \u0442\u043e \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u043c API \u0438 \u043a\u043b\u0430\u0434\u0435\u043c \u0432 \u043a\u0435\u0448         if (data === null) {             \/\/ cache for 5 minutes             data = await this.apiCall(payload);             await this.#client.set(payload, data, {                 EX: 60 * 5             });         }         return data;     }<\/code><\/pre>\n<p>\u0412 \u043a\u043e\u0434\u0435, \u043f\u0440\u0438 \u043e\u0431\u0440\u0430\u0449\u0435\u043d\u0438\u0438 \u043a \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u043c \u043c\u0435\u0442\u043e\u0434\u0430\u043c, \u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b await. \u0421\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e, \u043a\u0430\u043a \u0442\u043e\u043b\u044c\u043a\u043e \u0438\u0441\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u0432\u044b\u0437\u043e\u0432\u0430 cachedApiCall \u0434\u043e\u0439\u0434\u0435\u0442 \u0434\u043e \u044d\u0442\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0438 \u043e\u043d\u043e \u043f\u0440\u0435\u0440\u0432\u0435\u0442\u0441\u044f, \u0438 \u043d\u0430\u0447\u043d\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u044b\u0439 \u0432\u044b\u0437\u043e\u0432 (\u0430 \u0443 \u043d\u0430\u0441 \u0438\u0445 4). \u0422\u0430\u043a \u0431\u0443\u0434\u0435\u0442 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442\u044c \u043d\u0430 \u043a\u0430\u0436\u0434\u043e\u043c \u0432\u044b\u0437\u043e\u0432\u0435 await. \u0415\u0441\u043b\u0438 \u0431\u044b \u043d\u0430\u0448\u0435 \u043e\u0431\u0440\u0430\u0449\u0435\u043d\u0438\u0435 \u043a cachedApiCall \u043d\u0435 \u0432\u044b\u0437\u044b\u0432\u0430\u043b\u043e\u0441\u044c \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u043e, \u0442\u043e \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b \u0431\u044b \u0432 \u0442\u0430\u043a\u043e\u043c \u043a\u043e\u0434\u0435 \u043d\u0435 \u0431\u044b\u043b\u043e. \u041d\u043e \u043f\u0440\u0438 \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u043e\u043c \u0432\u044b\u0437\u043e\u0432\u0435 \u043c\u044b \u043d\u0430\u0440\u0432\u0430\u043b\u0438\u0441\u044c \u043d\u0430 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0433\u043e\u043d\u043a\u0438. \u0412\u0441\u0435 4 \u0432\u044b\u0437\u043e\u0432\u0430 \u0441\u043e\u0440\u0435\u0432\u043d\u0443\u044e\u0442\u0441\u044f \u0432\u043d\u0443\u0442\u0440\u0438 \u043c\u0435\u0442\u043e\u0434\u0430, \u0438 \u0432 \u0438\u0442\u043e\u0433\u0435 \u043c\u044b \u0438\u043c\u0435\u0435\u043c 4 \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u043d\u0430 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u043a\u0435\u0448\u0430, 4 \u0432\u044b\u0437\u043e\u0432\u0430 API, \u0438 4 \u0432\u044b\u0437\u043e\u0432\u0430 \u043d\u0430 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0443 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u043a\u0435\u0448\u0430.<\/p>\n<p>\u041a\u0430\u043a \u043c\u043e\u0436\u043d\u043e \u0440\u0435\u0448\u0438\u0442\u044c \u0442\u0430\u043a\u0443\u044e \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0443? \u041d\u0443\u0436\u043d\u043e \u043a\u0443\u0434\u0430-\u0442\u043e \u0441\u043f\u0440\u044f\u0442\u0430\u0442\u044c await, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u0432\u043e \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u043e\u0439 \u0444\u0443\u043a\u043d\u0446\u0438\u0438, \u0430 \u0441\u0430\u043c \u0432\u044b\u0437\u043e\u0432 \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043a\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432 \u043f\u0430\u043c\u044f\u0442\u0438 \u043d\u0430 \u0432\u0440\u0435\u043c\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u043e\u0441\u043d\u043e\u0432\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438.<\/p>\n<p>\u0412\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u044d\u0442\u043e \u0431\u0443\u0434\u0435\u0442 \u0432\u043e\u0442 \u0442\u0430\u043a:<\/p>\n<pre><code class=\"javascript\">    async cachedApiCall(payload) {         \/\/ \u0442\u0443\u0442 \u043c\u044b \u0441\u043f\u0440\u0430\u044f\u0442\u0430\u043b\u0438 \u043f\u0440\u043e\u0448\u043b\u044b\u0439 \u043a\u043e\u0434 \u0432\u043e \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e getOrSet         const getOrSet = async payload => {             let data = await this.#client.get(payload);             if (data === null) {                 \/\/ cache for 5 minutes                 data = await this.apiCall(payload);                 await this.#client.set(payload, data, {                     EX: 60 * 5                 });             }             return data;         }         \/\/ \u0432\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u043c \u043a\u0435\u0448\u0435 \u043d\u0430 \u0432\u0440\u0435\u043c\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043c\u044b \u0445\u0440\u0430\u043d\u0438\u043c \u043f\u0440\u043e\u043c\u0438\u0441\u044b         \/\/ \u0435\u0441\u043b\u0438 \u043d\u0430\u0445\u043e\u0434\u044f\u0441\u044c \u0442\u0443\u0442 \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0432 tempCache, \u0442\u043e \u043c\u044b \u0441\u043b\u043e\u0432\u0438\u043b\u0438         \/\/ \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u044b\u0439 \u0432\u044b\u0437\u043e\u0432         if (typeof this.#tempCache[payload] !== 'undefined')             return this.#tempCache[payload];         \/\/ \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u044f try - finally \u043d\u0430\u043c \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043f\u043e\u0447\u0438\u0441\u0442\u0438\u0442\u044c \u0437\u0430 \u0441\u043e\u0431\u043e\u0439 \u043f\u0440\u0438         \/\/ \u043b\u044e\u0431\u043e\u043c \u0438\u0441\u0445\u043e\u0434\u0435         try {             \/\/ \u043f\u043e\u043c\u0435\u0449\u0430\u0435\u043c \u0432\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0439 \u043a\u0435\u0448 \u043f\u0440\u043e\u043c\u0438\u0441             this.#tempCache[payload] = getOrSet(payload);             \/\/ \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c await, \u0447\u0442\u043e\u0431\u044b \u0432\u0441\u0435 \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u044b\u0435 \u0432\u044b\u0437\u043e\u0432\u044b \u0441\u044e\u0434\u0430 \u0437\u0430\u0448\u043b\u0438             return await this.#tempCache[payload];         } finally {             delete this.#tempCache[payload];         }     }<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c, \u043f\u0440\u0438 \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u043e\u043c \u0432\u044b\u0437\u043e\u0432\u0435 cachedApiCall \u0432\u0441\u0435 \u043f\u043e\u043b\u0443\u0447\u0430\u0442 \u043e\u0434\u0438\u043d \u0438 \u0442\u043e\u0442 \u0436\u0435 \u043f\u0440\u043e\u043c\u0438\u0441, \u0430 \u0437\u043d\u0430\u0447\u0438\u0442 \u0432\u044b\u0437\u043e\u0432\u044b \u043a Redis \u0438 \u043a \u043d\u0430\u0448\u0435\u043c\u0443 API \u043f\u0440\u043e\u0438\u0437\u043e\u0439\u0434\u0443\u0442 \u0432\u0441\u0435\u0433\u043e 1 \u0440\u0430\u0437.<\/p>\n<p>\u0411\u043e\u0440\u043e\u043b\u0438\u0441\u044c \u043b\u0438 \u0432\u044b \u0432 \u0441\u0432\u043e\u0438\u0445 JavaScript \u043f\u0440\u043e\u0435\u043a\u0442\u0430\u0445 \u0441 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\u043c \u0433\u043e\u043d\u043a\u0438 \u0438 \u043a\u0430\u043a\u0438\u0435 \u043f\u043e\u0434\u0445\u043e\u0434\u044b \u0432\u044b \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u043b\u0438?<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/582664\/\"> https:\/\/habr.com\/ru\/articles\/582664\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0443\u044e \u0437\u0430\u0434\u0430\u0447\u0443. \u041d\u0430\u043c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0437\u043e\u0432\u044b \u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0433\u043e API, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0441\u0447\u0438\u0442\u0430\u044e\u0442\u0441\u044f \u0434\u043e\u0440\u043e\u0433\u0438\u043c\u0438, \u0438, \u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e, \u0438\u0445 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043a\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432 Redis. \u041c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u0441\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0439 NodeJS (\u0432\u0435\u0440\u0441\u0438\u0438 14+), \u0430 \u0437\u043d\u0430\u0447\u0438\u0442 \u0438 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u0438 async \/ await.<\/p>\n<p>\u041d\u0430\u043f\u0438\u0448\u0435\u043c \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u043a\u043b\u0430\u0441\u0441 \u043e\u0431\u0435\u0440\u0442\u043a\u0443 \u043d\u0430\u0434 \u0432\u044b\u0437\u043e\u0432\u043e\u043c API, \u0433\u0434\u0435 \u0441\u0430\u043c \u0432\u044b\u0437\u043e\u0432 \u0431\u0443\u0434\u0435\u043c \u044d\u043c\u0443\u043b\u0438\u0440\u043e\u0432\u0430\u0442\u044c 2-\u0441\u0435\u043a\u0443\u043d\u0434\u043d\u044b\u043c \u0442\u0430\u0439\u043c\u0430\u0443\u0442\u043e\u043c.<\/p>\n<pre><code class=\"javascript\">class ApiWrapper {     #callTimes = 0;      async apiCall(payload) {         return new Promise(resolve => {             setTimeout(() => {                 this.#callTimes++;                 resolve(`success: ${payload}`)             }, 2000);         })     }      get callTimes() {         return this.#callTimes;     } }  const run = async () => {     const api = new ApiWrapper();      \/\/ \u044d\u043c\u0443\u043b\u0438\u0440\u0443\u0435\u043c \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u044b\u0439 \u0432\u044b\u0437\u043e\u0432 API 4 \u0440\u0430\u0437\u0430     const prDirect = await Promise.all([api.apiCall('test'), api.apiCall('test'), api.apiCall('test'), api.apiCall('test')]);     console.log(prDirect); \/\/ => ['success: test', 'success: test', 'success: test', 'success: test']     console.log(apiCache.callTimes); \/\/ => 4 }  run();<\/code><\/pre>\n<p>\u042f \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u044c\u043d\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u043b \u0432 \u043a\u043b\u0430\u0441\u0441 \u0441\u0447\u0435\u0442\u0447\u0438\u043a \u0432\u044b\u0437\u043e\u0432\u043e\u0432 callTimes &#8212; \u043e\u043d \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0440\u0430\u0437 \u043c\u044b \u0432\u044b\u0437\u0432\u0430\u043b\u0438 \u043c\u0435\u0442\u043e\u0434 API. \u0412 \u0434\u0430\u043d\u043d\u043e\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u0443 \u043d\u0430\u0441 4 \u043f\u0440\u044f\u043c\u044b\u0445 \u0432\u044b\u0437\u043e\u0432\u0430.<\/p>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u043a \u043a\u043e\u0434\u0443 \u043a\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0432 Redis. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043f\u0430\u043a\u0435\u0442 redis@next.<\/p>\n<details class=\"spoiler\">\n<summary>\u041a\u043e\u0434 \u0441 \u043a\u0435\u0448\u0438\u0440\u0443\u044e\u0449\u0438\u043c \u043c\u0435\u0442\u043e\u0434\u043e\u043c cachedApiCall<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"javascript\">class ApiWrapper {     #client;     #callTimes = 0;      constructor(url) {         \/\/ \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u043a\u043b\u0438\u0435\u043d\u0442\u0430 Redis         this.#client = createClient({             url         });     }      async init() {         \/\/ \u041f\u043e\u0434\u043a\u043b\u044e\u0447\u0430\u0435\u043c\u0441\u044f \u043a Redis         await this.#client.connect();     }      async apiCall(payload) {         return new Promise(resolve => {             setTimeout(() => {                 this.#callTimes++;                 resolve(`success: ${payload}`)             }, 2000);         })     }      async cachedApiCall(payload) {         let data = await this.#client.get(payload);         if (data === null) {             \/\/ cache for 5 minutes             data = await this.apiCall(payload);             await this.#client.set(payload, data, {                 EX: 60 * 5             });         }         return data;     }      get callTimes() {         return this.#callTimes;     } }  const run = async () => {     const api = new ApiWrapper('redis:\/\/10.250.200.9:6379\/6');     await api.init();      const prCached = await Promise.all([api.cachedApiCall('test'), api.cachedApiCall('test'), api.cachedApiCall('test'), api.cachedApiCall('test')]);     console.log(prCached); \/\/ => ['success: test', 'success: test', 'success: test', 'success: test']     console.log(api.callTimes); \/\/ => 4 }<\/code><\/pre>\n<\/div>\n<\/details>\n<p>\u041d\u0435\u0441\u043c\u043e\u0442\u0440\u044f \u043d\u0430 \u0442\u043e, \u0447\u0442\u043e \u043c\u044b \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u043c cachedApiCall, \u043d\u0430\u0448 \u0441\u0447\u0435\u0442\u0447\u0438\u043a \u0432\u044b\u0437\u043e\u0432\u043e\u0432 API \u0432\u0441\u0451 \u0440\u0430\u0432\u043d\u043e \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u0446\u0438\u0444\u0440\u0443 4. \u042d\u0442\u043e \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u0438\u0437-\u0437\u0430 \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e\u0441\u0442\u0435\u0439 \u0440\u0430\u0431\u043e\u0442\u044b async \/ await.<\/p>\n<p>\u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0434\u0435\u0442\u0430\u043b\u044c\u043d\u0435\u0439 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043a\u0435\u0448\u0438\u0440\u0443\u044e\u0449\u0438\u0439 \u043c\u0435\u0442\u043e\u0434. \u042f \u0435\u0433\u043e \u043d\u0430\u043f\u0438\u0441\u0430\u043b \u0442\u0430\u043a, \u043a\u0430\u043a \u0435\u0441\u043b\u0438 \u0431\u044b \u044f \u0440\u0430\u0431\u043e\u0442\u0430\u043b \u0441 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u043c \u043a\u043e\u0434\u043e\u043c.<\/p>\n<pre><code class=\"javascript\">    async cachedApiCall(payload) {         \/\/ \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0434\u0430\u043d\u043d\u044b\u0435 \u0438\u0437 \u043a\u0435\u0448\u0430         let data = await this.#client.get(payload);         \/\/ \u0435\u0441\u043b\u0438 \u0434\u0430\u043d\u043d\u044b\u0445 \u043d\u0435\u0442, \u0442\u043e \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u043c API \u0438 \u043a\u043b\u0430\u0434\u0435\u043c \u0432 \u043a\u0435\u0448         if (data === null) {             \/\/ cache for 5 minutes             data = await this.apiCall(payload);             await this.#client.set(payload, data, {                 EX: 60 * 5             });         }         return data;     }<\/code><\/pre>\n<p>\u0412 \u043a\u043e\u0434\u0435, \u043f\u0440\u0438 \u043e\u0431\u0440\u0430\u0449\u0435\u043d\u0438\u0438 \u043a \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u043c \u043c\u0435\u0442\u043e\u0434\u0430\u043c, \u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b await. \u0421\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e, \u043a\u0430\u043a \u0442\u043e\u043b\u044c\u043a\u043e \u0438\u0441\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u0432\u044b\u0437\u043e\u0432\u0430 cachedApiCall \u0434\u043e\u0439\u0434\u0435\u0442 \u0434\u043e \u044d\u0442\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0438 \u043e\u043d\u043e \u043f\u0440\u0435\u0440\u0432\u0435\u0442\u0441\u044f, \u0438 \u043d\u0430\u0447\u043d\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u044b\u0439 \u0432\u044b\u0437\u043e\u0432 (\u0430 \u0443 \u043d\u0430\u0441 \u0438\u0445 4). \u0422\u0430\u043a \u0431\u0443\u0434\u0435\u0442 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442\u044c \u043d\u0430 \u043a\u0430\u0436\u0434\u043e\u043c \u0432\u044b\u0437\u043e\u0432\u0435 await. \u0415\u0441\u043b\u0438 \u0431\u044b \u043d\u0430\u0448\u0435 \u043e\u0431\u0440\u0430\u0449\u0435\u043d\u0438\u0435 \u043a cachedApiCall \u043d\u0435 \u0432\u044b\u0437\u044b\u0432\u0430\u043b\u043e\u0441\u044c \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u043e, \u0442\u043e \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b \u0431\u044b \u0432 \u0442\u0430\u043a\u043e\u043c \u043a\u043e\u0434\u0435 \u043d\u0435 \u0431\u044b\u043b\u043e. \u041d\u043e \u043f\u0440\u0438 \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u043e\u043c \u0432\u044b\u0437\u043e\u0432\u0435 \u043c\u044b \u043d\u0430\u0440\u0432\u0430\u043b\u0438\u0441\u044c \u043d\u0430 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0433\u043e\u043d\u043a\u0438. \u0412\u0441\u0435 4 \u0432\u044b\u0437\u043e\u0432\u0430 \u0441\u043e\u0440\u0435\u0432\u043d\u0443\u044e\u0442\u0441\u044f \u0432\u043d\u0443\u0442\u0440\u0438 \u043c\u0435\u0442\u043e\u0434\u0430, \u0438 \u0432 \u0438\u0442\u043e\u0433\u0435 \u043c\u044b \u0438\u043c\u0435\u0435\u043c 4 \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u043d\u0430 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u043a\u0435\u0448\u0430, 4 \u0432\u044b\u0437\u043e\u0432\u0430 API, \u0438 4 \u0432\u044b\u0437\u043e\u0432\u0430 \u043d\u0430 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0443 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u043a\u0435\u0448\u0430.<\/p>\n<p>\u041a\u0430\u043a \u043c\u043e\u0436\u043d\u043e \u0440\u0435\u0448\u0438\u0442\u044c \u0442\u0430\u043a\u0443\u044e \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0443? \u041d\u0443\u0436\u043d\u043e \u043a\u0443\u0434\u0430-\u0442\u043e \u0441\u043f\u0440\u044f\u0442\u0430\u0442\u044c await, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u0432\u043e \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u043e\u0439 \u0444\u0443\u043a\u043d\u0446\u0438\u0438, \u0430 \u0441\u0430\u043c \u0432\u044b\u0437\u043e\u0432 \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043a\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432 \u043f\u0430\u043c\u044f\u0442\u0438 \u043d\u0430 \u0432\u0440\u0435\u043c\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u043e\u0441\u043d\u043e\u0432\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438.<\/p>\n<p>\u0412\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u044d\u0442\u043e \u0431\u0443\u0434\u0435\u0442 \u0432\u043e\u0442 \u0442\u0430\u043a:<\/p>\n<pre><code class=\"javascript\">    async cachedApiCall(payload) {         \/\/ \u0442\u0443\u0442 \u043c\u044b \u0441\u043f\u0440\u0430\u044f\u0442\u0430\u043b\u0438 \u043f\u0440\u043e\u0448\u043b\u044b\u0439 \u043a\u043e\u0434 \u0432\u043e \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e getOrSet         const getOrSet = async payload => {             let data = await this.#client.get(payload);             if (data === null) {                 \/\/ cache for 5 minutes                 data = await this.apiCall(payload);                 await this.#client.set(payload, data, {                     EX: 60 * 5                 });             }             return data;         }         \/\/ \u0432\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u043c \u043a\u0435\u0448\u0435 \u043d\u0430 \u0432\u0440\u0435\u043c\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043c\u044b \u0445\u0440\u0430\u043d\u0438\u043c \u043f\u0440\u043e\u043c\u0438\u0441\u044b         \/\/ \u0435\u0441\u043b\u0438 \u043d\u0430\u0445\u043e\u0434\u044f\u0441\u044c \u0442\u0443\u0442 \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0432 tempCache, \u0442\u043e \u043c\u044b \u0441\u043b\u043e\u0432\u0438\u043b\u0438         \/\/ \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u044b\u0439 \u0432\u044b\u0437\u043e\u0432         if (typeof this.#tempCache[payload] !== 'undefined')             return this.#tempCache[payload];         \/\/ \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u044f try - finally \u043d\u0430\u043c \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043f\u043e\u0447\u0438\u0441\u0442\u0438\u0442\u044c \u0437\u0430 \u0441\u043e\u0431\u043e\u0439 \u043f\u0440\u0438         \/\/ \u043b\u044e\u0431\u043e\u043c \u0438\u0441\u0445\u043e\u0434\u0435         try {             \/\/ \u043f\u043e\u043c\u0435\u0449\u0430\u0435\u043c \u0432\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0439 \u043a\u0435\u0448 \u043f\u0440\u043e\u043c\u0438\u0441             this.#tempCache[payload] = getOrSet(payload);             \/\/ \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c await, \u0447\u0442\u043e\u0431\u044b \u0432\u0441\u0435 \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u044b\u0435 \u0432\u044b\u0437\u043e\u0432\u044b \u0441\u044e\u0434\u0430 \u0437\u0430\u0448\u043b\u0438             return await this.#tempCache[payload];         } finally {             delete this.#tempCache[payload];         }     }<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c, \u043f\u0440\u0438 \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u043e\u043c \u0432\u044b\u0437\u043e\u0432\u0435 cachedApiCall \u0432\u0441\u0435 \u043f\u043e\u043b\u0443\u0447\u0430\u0442 \u043e\u0434\u0438\u043d \u0438 \u0442\u043e\u0442 \u0436\u0435 \u043f\u0440\u043e\u043c\u0438\u0441, \u0430 \u0437\u043d\u0430\u0447\u0438\u0442 \u0432\u044b\u0437\u043e\u0432\u044b \u043a Redis \u0438 \u043a \u043d\u0430\u0448\u0435\u043c\u0443 API \u043f\u0440\u043e\u0438\u0437\u043e\u0439\u0434\u0443\u0442 \u0432\u0441\u0435\u0433\u043e 1 \u0440\u0430\u0437.<\/p>\n<p>\u0411\u043e\u0440\u043e\u043b\u0438\u0441\u044c \u043b\u0438 \u0432\u044b \u0432 \u0441\u0432\u043e\u0438\u0445 JavaScript \u043f\u0440\u043e\u0435\u043a\u0442\u0430\u0445 \u0441 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\u043c \u0433\u043e\u043d\u043a\u0438 \u0438 \u043a\u0430\u043a\u0438\u0435 \u043f\u043e\u0434\u0445\u043e\u0434\u044b \u0432\u044b \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u043b\u0438?<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/582664\/\"> https:\/\/habr.com\/ru\/articles\/582664\/<\/a><br \/><\/br><\/br><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-411394","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/411394","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=411394"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/411394\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=411394"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=411394"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=411394"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}