From 4a847ddffa31cafe4ea9b43182d5d6e08b219978 Mon Sep 17 00:00:00 2001 From: m0_75226990 Date: Mon, 19 Aug 2024 16:41:51 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9Epromise=E9=9D=99=E6=80=81?= =?UTF-8?q?=E5=87=BD=E6=95=B0=E6=B5=8B=E8=AF=95=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages/SyntaxCase/index.uvue | 17 +++- uni_modules/uts-syntaxcase/utssdk/index.uts | 101 ++++++++++++++++++++ 2 files changed, 116 insertions(+), 2 deletions(-) diff --git a/pages/SyntaxCase/index.uvue b/pages/SyntaxCase/index.uvue index e3322db..8d16dba 100644 --- a/pages/SyntaxCase/index.uvue +++ b/pages/SyntaxCase/index.uvue @@ -53,7 +53,10 @@ 测试callback:{{ format(testUtsClassInstanceResult['callback']) }} 测试setter:{{ format(testUtsClassSetterResult) }} - + + + + @@ -73,6 +76,11 @@ request, SetterTest, } from "../../uni_modules/uts-syntaxcase"; + // #endif + // #ifdef APP-IOS + import { + testPromiseStaticMethod + } from "@/uni_modules/uts-syntaxcase" // #endif let test:Test|null = null let id = 0 @@ -441,7 +449,12 @@ } catch (e) { console.error("testUtsClassSetter", e); } - }, + }, + // #ifdef APP-IOS + testPromiseStaticMethod() { + testPromiseStaticMethod() + } + // #endif } } \ No newline at end of file diff --git a/uni_modules/uts-syntaxcase/utssdk/index.uts b/uni_modules/uts-syntaxcase/utssdk/index.uts index ab501a9..b9bc6bf 100644 --- a/uni_modules/uts-syntaxcase/utssdk/index.uts +++ b/uni_modules/uts-syntaxcase/utssdk/index.uts @@ -1,6 +1,9 @@ import { RequestTask, SyncOptions, AsyncOptions, SyntaxResult, SyncResult, TestOptions } from "./interface.uts"; import { log } from "./utils.uts"; +// #ifdef APP-IOS +import { UTSPromiseAggregateError, UTSPromiseFulfilledResult, UTSPromiseRejectedResult} from "DCloudUTSFoundation"; +// #endif /** * 导出一个属性 @@ -121,6 +124,104 @@ export class Test { } } +// #ifdef APP-IOS +// #ifdef UNI-APP-X + +export function testPromiseStaticMethod() { + let p0 = new Promise((resolve, reject) => { + let success = true + setTimeout(() => { + if (success) { + const res : SyntaxResult = { + name: "pomise 0", + } + resolve(res); + } else{ + reject(new Error("this is promise 0 reject reasion")) + } + + }, 1000); + }); + + let p1 = new Promise((resolve, reject) => { + let success = true + setTimeout(() => { + if (success) { + resolve(null); + } else{ + reject(new Error("this is promise 1 reject reasion")) + } + + }, 2000); + }); + + let p2 = new Promise((resolve, reject) => { + let success = false + setTimeout(() => { + if (success) { + resolve(null); + } else{ + // reject(new Error("this is promise 2 reject reasion")) + reject(null) + } + + }, 2500); + }); + + // test for any + let pt = Promise.any([p0, p1, p2]); + pt.then( (res) => { + console.log("promise.any test success", res) + }) + .catch( (error: any | null) => { + if (error instanceof UTSPromiseAggregateError) { + let err = error as UTSPromiseAggregateError + console.log(err.name, err.message, err.errors); + }else { + console.log(error); + } + }) + + // test for allSettled + Promise.allSettled([p0, p1, p2]) + .then((res) => { + res.forEach((item, index) => { + if (item instanceof UTSPromiseFulfilledResult) { + let item0 = item as UTSPromiseFulfilledResult + console.log(item0.value, "UTSPromiseFulfilledResult value") + } else if (item instanceof UTSPromiseRejectedResult) { + let item0 = item as UTSPromiseRejectedResult + console.log(item0.reason, "UTSPromiseRejectedResult reason") + } + }) + }) + .catch((error: any | null) => { + console.log(error, "rejected test for promise.allSettled") + }) + + // test for all + Promise.all([p0, p1, p2]) + .then((res)=> { + console.log(res, res.count) + }) + .catch( (error: any | null) => { + console.log(error, "rejected test for promise.all") + }) + + // test for race + Promise.race([p0, p1, p2]) + .then((res) => { + console.log(res, "resolved test for promise.race"); + }) + .catch((error: any | null) => { + console.log(error, "catch test for promise.race") + }) +} + + +// #endif +// #endif + export class SetterTest { private _nickName: string = ''; -- GitLab