es7-test.es7.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /**
  2. * Created by fy on 15-10-5.
  3. */
  4. 'use strict';
  5. import "babel/polyfill";
  6. async function sleep(timeout) {
  7. return new Promise((resolve, reject) => {
  8. setTimeout(function () {
  9. resolve();
  10. }, timeout);
  11. });
  12. }
  13. (async function () {
  14. console.log('Do some thing, ' + new Date());
  15. await sleep(1000);
  16. console.log('Do other things, ' + new Date());
  17. })();
  18. import fs from "fs";
  19. import prominence from "prominence";
  20. var filepath = '/home/fy/workspaces/laotang/laotang_zsy_sb_manage/PROCESS_ID';
  21. // use Prominence Proxy
  22. var text = prominence(fs).readFile(filepath, "utf-8").catch(console.error.bind(console));
  23. console.log(text);
  24. // not use a proxy
  25. prominence(fs, "readFile", [filepath, "utf-8"]).then((text) => {
  26. console.log(text);
  27. }).catch(console.error.bind(console));
  28. var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
  29. var fetchDoubanApi = function () {
  30. return new Promise((resolve, reject) => {
  31. var xhr = new XMLHttpRequest();
  32. xhr.onreadystatechange = function () {
  33. if (xhr.readyState === 4) {
  34. if (xhr.status >= 200 && xhr.status < 300) {
  35. var response;
  36. try {
  37. response = JSON.parse(xhr.responseText);
  38. } catch (e) {
  39. reject(e);
  40. }
  41. if (response) {
  42. resolve(response, xhr.status, xhr);
  43. }
  44. } else {
  45. reject(xhr);
  46. }
  47. }
  48. };
  49. xhr.open('GET', 'https://api.douban.com/v2/user/aisk', true);
  50. xhr.setRequestHeader("Content-Type", "text/plain");
  51. var data = null;
  52. xhr.send(data);
  53. });
  54. };
  55. (async function () {
  56. try {
  57. let result = await fetchDoubanApi();
  58. console.log('---------------------------------->>\n', result);
  59. } catch (e) {
  60. console.log(e);
  61. }
  62. })();