Mercurial > hg > nginx-site
comparison xml/en/docs/njs/examples.xml @ 2799:24b379907b0f
Added async and await to njs Examples.
author | Yaroslav Zhuravlev <yar@nginx.com> |
---|---|
date | Fri, 19 Nov 2021 08:19:57 +0000 |
parents | bf641527bd3d |
children | 9eadb98ec770 |
comparison
equal
deleted
inserted
replaced
2798:cf75284a3557 | 2799:24b379907b0f |
---|---|
7 <!DOCTYPE article SYSTEM "../../../../dtd/article.dtd"> | 7 <!DOCTYPE article SYSTEM "../../../../dtd/article.dtd"> |
8 | 8 |
9 <article name="Examples" | 9 <article name="Examples" |
10 link="/en/docs/njs/examples.html" | 10 link="/en/docs/njs/examples.html" |
11 lang="en" | 11 lang="en" |
12 rev="20"> | 12 rev="21"> |
13 | 13 |
14 <section id="summary"> | 14 <section id="summary"> |
15 | 15 |
16 <para> | 16 <para> |
17 The examples work since | 17 The examples work since |
223 </para> | 223 </para> |
224 | 224 |
225 <para> | 225 <para> |
226 <path>http.js</path>: | 226 <path>http.js</path>: |
227 <example> | 227 <example> |
228 function set_keyval(r) { | 228 async function set_keyval(r) { |
229 r.subrequest('/api/7/http/keyvals/foo', | 229 let res = await r.subrequest('/api/7/http/keyvals/foo', |
230 { method: 'POST', | 230 { method: 'POST', |
231 body: JSON.stringify({ foo: 789, bar: "ss dd 00" })}, | 231 body: JSON.stringify({ foo: 789, bar: "ss dd 00" })}); |
232 | 232 |
233 function(res) { | 233 if (res.status >= 300) { |
234 if (res.status >= 300) { | 234 r.return(res.status, res.responseBody); |
235 r.return(res.status, res.responseBody); | 235 return; |
236 return; | 236 } |
237 } | 237 |
238 r.return(500); | 238 r.return(200); |
239 }); | 239 } |
240 } | 240 |
241 | 241 async function version(r) { |
242 function version(r) { | 242 let res = await r.subrequest('/api/7/nginx', { method: 'GET' }); |
243 r.subrequest('/api/7/nginx', { method: 'GET' }, function(res) { | 243 |
244 if (res.status != 200) { | 244 if (res.status != 200) { |
245 r.return(res.status); | 245 r.return(res.status); |
246 return; | 246 return; |
247 } | 247 } |
248 | 248 |
249 var json = JSON.parse(res.responseBody); | 249 var json = JSON.parse(res.responseBody); |
250 r.return(200, json.version); | 250 r.return(200, json.version); |
251 }); | |
252 } | 251 } |
253 | 252 |
254 export default {set_keyval, version}; | 253 export default {set_keyval, version}; |
255 </example> | 254 </example> |
256 <note> | 255 <note> |
331 </para> | 330 </para> |
332 | 331 |
333 <para> | 332 <para> |
334 <path>http.js</path>: | 333 <path>http.js</path>: |
335 <example> | 334 <example> |
336 function content(r) { | 335 async function content(r) { |
337 r.subrequest('/auth') | 336 try { |
338 .then(reply => JSON.parse(reply.responseBody)) | 337 let reply = await r.subrequest('/auth'); |
339 .then(response => { | 338 let response = JSON.parse(reply.responseBody); |
340 if (!response['token']) { | 339 let token = response['token']; |
341 throw new Error("token is not available"); | 340 |
342 } | 341 if (!token) { |
343 return reply['token']; | 342 throw new Error("token is not available"); |
344 }) | 343 } |
345 .then(token => { | 344 |
346 r.subrequest('/backend', `token=${token}`) | 345 let backend_reply = await r.subrequest('/backend', `token=${token}`); |
347 .then(reply => r.return(reply.status, reply.responseBody)); | 346 r.return(backend_reply.status, backend_reply.responseBody); |
348 }) | 347 |
349 .catch(_ => r.return(500)); | 348 } catch (e) { |
349 r.error(e); | |
350 r.return(500); | |
351 } | |
350 } | 352 } |
351 | 353 |
352 export default {content}; | 354 export default {content}; |
353 </example> | 355 </example> |
354 </para> | 356 </para> |