使用 async/await 语法

我们可以使用最新的 ES8(ES2017)功能,并使用 async / await 语法代替:

  1. import { createConnection } from "typeorm";
  2. import { Photo } from "./entity/Photo";
  3. createConnection(/*...*/)
  4. .then(async connection => {
  5. let photo = new Photo();
  6. photo.name = "Me and Bears";
  7. photo.description = "I am near polar bears";
  8. photo.filename = "photo-with-bears.jpg";
  9. photo.views = 1;
  10. photo.isPublished = true;
  11. await connection.manager.save(photo);
  12. console.log("Photo has been saved");
  13. })
  14. .catch(error => console.log(error));