提交 4a847ddf 编写于 作者: lizhongyi_'s avatar lizhongyi_

新增promise静态函数测试例

上级 c0567af5
......@@ -53,7 +53,10 @@
<view>测试callback:{{ format(testUtsClassInstanceResult['callback']) }}</view>
<button @click="testUtsClassSetter">点击测试class 示例setter方法</button>
<view>测试setter:{{ format(testUtsClassSetterResult) }}</view>
<button @click="testAll">点击测试所有</button>
<button @click="testAll">点击测试所有</button>
<!-- #ifdef APP-IOS -->
<button @click="testPromiseStaticMethod">点击测试promise静态方法</button>
<!-- #endif -->
</view>
<view style="height: 20px;"></view>
</scroll-view>
......@@ -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
}
}
</script>
\ No newline at end of file
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<SyntaxResult | null>((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<SyntaxResult | null>((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<SyntaxResult | null>((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<SyntaxResult | null >) {
let item0 = item as UTSPromiseFulfilledResult
console.log(item0.value, "UTSPromiseFulfilledResult value")
} else if (item instanceof UTSPromiseRejectedResult<SyntaxResult | null>) {
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 = '';
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册