asyncAwaitBasic.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. var log;
  6. if (typeof telemetryLog !== 'undefined') {
  7. log = telemetryLog;
  8. }
  9. else if (typeof console !== 'undefined' && typeof console.log !== 'undefined') {
  10. log = function (msg, shouldWrite) {
  11. if (shouldWrite) {
  12. console.log(msg);
  13. }
  14. }
  15. }
  16. else {
  17. log = function (msg, shouldWrite) {
  18. if (shouldWrite) {
  19. WScript.Echo(msg);
  20. }
  21. };
  22. }
  23. var setTimeoutX;
  24. if (typeof setTimeout !== 'undefined') {
  25. setTimeoutX = setTimeout;
  26. }
  27. else {
  28. setTimeoutX = WScript.SetTimeout;
  29. }
  30. var writeTTDLog;
  31. if (typeof emitTTDLog === 'undefined') {
  32. writeTTDLog = function (uri) {
  33. // no-op
  34. };
  35. }
  36. else {
  37. writeTTDLog = emitTTDLog;
  38. }
  39. var ttdLogUriX;
  40. if (typeof ttdLogURI !== 'undefined') {
  41. ttdLogUriX = ttdLogURI;
  42. }
  43. /////////////////
  44. async function f1(a, b, c) {
  45. log('f1 starting', true);
  46. return { a, b, c };
  47. }
  48. async function f2(d, e, f) {
  49. log('f2 starting', true);
  50. let x = await f1(d + 10, e + 20, f + 30);
  51. return x;
  52. }
  53. async function f3() {
  54. log('f3 starting', true);
  55. var x = await f2(1, 2, 3);
  56. var xstr = JSON.stringify(x, undefined, 0);
  57. log(`x = ${xstr}`, true);
  58. }
  59. setTimeoutX(() => {
  60. f3().then(() => {
  61. log('done', true);
  62. writeTTDLog(ttdLogUriX);
  63. }, (err) => {
  64. log(`error: ${err}`, true);
  65. writeTTDLog(ttdLogUriX);
  66. })
  67. }, 20);