- 使用 async/await 语法
使用 async/await 语法
我们可以使用最新的 ES8(ES2017)功能,并使用 async / await 语法代替:
import { createConnection } from "typeorm";
import { Photo } from "./entity/Photo";
createConnection(/*...*/)
.then(async connection => {
let photo = new Photo();
photo.name = "Me and Bears";
photo.description = "I am near polar bears";
photo.filename = "photo-with-bears.jpg";
photo.views = 1;
photo.isPublished = true;
await connection.manager.save(photo);
console.log("Photo has been saved");
})
.catch(error => console.log(error));