Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- async function sleep(ms: number) {
- return new Promise((resolve, reject) => {
- setTimeout(() => resolve(), ms)
- })
- }
- async function randomDelay() {
- const randomTime = Math.round(Math.random() * 1000)
- return sleep(randomTime)
- }
- class ShipmentSearchIndex {
- async updateShipment(id: string, shipmentData: any) {
- const startTime = new Date()
- await randomDelay()
- const endTime = new Date()
- console.log(`update ${id}@${
- startTime.toISOString()
- } finished@${
- endTime.toISOString()
- }`
- )
- return {startTime, endTime}
- }
- }
- interface ShipmentUpdateListenerInterface {
- receiveUpdate(id: string, shipmentData: any)
- }
- class ShipmentUpdater implements ShipmentUpdateListenerInterface{
- private shipmentSearchIndex: ShipmentSearchIndex = new ShipmentSearchIndex();;
- async receiveUpdate(id: string, shipmentData: any) {
- await this.shipmentSearchIndex.updateShipment(id, shipmentData);
- }
- }
- async function testClass(){
- let updater: ShipmentUpdater = new ShipmentUpdater();
- await updater.receiveUpdate('1', {});
- await updater.receiveUpdate('2', {});
- }
- testClass();
Add Comment
Please, Sign In to add comment