From 2dccc8f9c2acbb2f77bc69f99fc042633572b457 Mon Sep 17 00:00:00 2001 From: m0_75226990 Date: Tue, 27 Aug 2024 16:10:10 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9Epromise=20=E9=83=A8=E5=88=86?= =?UTF-8?q?=E6=B5=8B=E8=AF=95=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- uni_modules/uts-tests/utssdk/Promise.uts | 71 +++++++++++++++++++++++- 1 file changed, 68 insertions(+), 3 deletions(-) diff --git a/uni_modules/uts-tests/utssdk/Promise.uts b/uni_modules/uts-tests/utssdk/Promise.uts index 954551f..8e6e69d 100644 --- a/uni_modules/uts-tests/utssdk/Promise.uts +++ b/uni_modules/uts-tests/utssdk/Promise.uts @@ -5,7 +5,7 @@ export function testPromise(): Result { // #ifdef APP-IOS test('constructor', () => { // 常规方式 - let p = new Promise((resolve, reject) => { + let p = new Promise((resolve, reject) => { let success = true; setTimeout(()=>{ if (success) { @@ -16,7 +16,7 @@ export function testPromise(): Result { } }, 10); }); - p.then((res: Int) => { + p.then((res: number) => { expect(res).toEqual(2024) }) @@ -92,7 +92,44 @@ export function testPromise(): Result { }) test('catch', () => { - + let p = Promise.reject() + p.then() + .catch((res: any | null) => { + expect(res).toEqual(null) + }) + + // catch 指定返回值 + new Promise((resolve, reject) => { + setTimeout(() => { + reject("2024") + }, 12) + }) + .then() + .catch((res: any | null): string => { + expect(res!).toEqual("2024") + return "hello world" + }) + .then((res) => { + expect(res).toEqual("hello world") + }) + + // catch 返回Promise + new Promise((resolve, reject) => { + setTimeout(() => { + reject("2024") + }, 12) + }) + .then() + .catch((res: any | null): Promise => { + expect(res!).toEqual("2024") + let p = new Promise((resolve, reject) => { + resolve(1) + }) + return p + }) + .then((res) => { + expect(res).toEqual(1) + }) }) test('finally', () => { @@ -111,6 +148,34 @@ export function testPromise(): Result { .catch((res) => { expect(res).toEqual(3) }) + + Promise.resolve(2) + .finally((): number => { + console.log("finally") + return 88 + }) + .then((res) => { + expect(res).toEqual(2) + }) + + Promise.reject(3).finally(() => { + throw new Error("99") + }) + .catch((res) => { + expect((res as Error).message).toEqual("99"); + }) + + console.log("start"); + Promise.reject(4).finally((): Promise => { + return new Promise((resolve, reject) => { + setTimeout(() => { + resolve(20) + }, 1000) + }) + }) + .catch((res) => { + expect(res).toEqual(4) + }) }) test('resolve', () => { -- GitLab