c5_labsci/fapp/ciyon_ap/pages/demo/case/promise.vue

63 lines
1.4 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<template>
<ciy-header title="Promise极简示例"></ciy-header>
<view>
<view class="txt-center py4">
<button class="btn long" @tap="prom(true)">请求Promise success</button>
</view>
<view class="txt-center py4">
<button class="btn long" @tap="prom(false)">请求Promise fail</button>
</view>
<view class="ciy-card" style="padding:1em;">
Result: {{msg}}
</view>
<view class="px4 py1" v-for="item in log" :key="item">{{item}}</view>
</view>
</template>
<style>
</style>
<script>
export default {
data() {
return {
msg: '--',
log: []
}
},
onLoad() {},
watch: {},
computed: {},
methods: {
async prom(ret) {
this.msg = '... ...';
var res = await this.dopromise({
ret: ret
});
this.log.unshift('prom: ' + res);
console.log('prom', res);
this.msg = res ? res : 'undefined';
},
async dopromise(opt) {
return new Promise((resolve, reject) => {
this.log.unshift('run 1');
console.log('run 1');
setTimeout(() => {
if (opt.ret)
resolve('success');
else
return reject('fail');
this.log.unshift('result 3');
console.log('result 3');
}, 1000);
this.log.unshift('run 2');
console.log('run 2');
}).catch(e => {
this.log.unshift('catch 4: ' + e);
console.log('catch 4', e);
return e; //不返回res为 undefined
});
}
}
}
</script>