promise的运行

news/2024/7/19 14:24:20 标签: js, vue
const promise = new Promise(function(resolve, reject){
    setTimeout(function(){
        try {
            let c = 6 / 2 ;
            resolve(c);
            console.log(c)
        }catch(ex) {
            reject(ex);
            
        }
    }, 1000)
})
promise.then(function(value) {
    console.log(value)
},function(err){
    console.error(err.message)
})

运行结果是3

catch的使用

const promise = new Promise(function(resolve, reject){
    setTimeout(function(){
        try {
            let c = 6 / 2 ;
            resolve(c);
            console.log(c)
        }catch(ex) {
            reject(ex);
            
        }
    }, 1000)
})
promise.then(function(value) {
    console.log(value)
}) .catch(function (err) {
    console.error(err.message);
})

拒绝的情形

const promise = new Promise(function(resolve, reject){
    setTimeout(function(){
        try {
            let c = 6 / 0 ;
            resolve(c);
            console.log(c)
        }catch(ex) {
            reject(ex);
            
        }
    }, 1000)
})
promise.then(function(value) {
    console.log(value)
}) .catch(function (err) {
    console.error(err.message);
})

输出
Infinity

没有用catch也输出的是一样啊

const promise = new Promise(function(resolve, reject){
    setTimeout(function(){
        try {
            let c = 6 / 0 ;
            resolve(c);
            console.log(c)
        }catch(ex) {
            reject(ex);
            
        }
    }, 1000)
})
promise.then(function(value) {
    console.log(value)
},function(err){
    console.error(err.message)
})


http://www.niftyadmin.cn/n/957430.html

相关文章

说有bug,但是好像没有啊,没看出来哪里可以改

const promise new Promise((resolve, reject) > {setTimeout( () > {let inArray new Array(20);for(let i0; i<20; i) {intArray[i] parseInt(Math.random() * 20, 10);resolve(inArray);}, 1000);console.log("开始生成一个随机的数组")});promise.th…

算法学习(六)——alpha Zero模型

论文&#xff1a; https://xueshu.baidu.com/usercenter/paper/show?paperiddba68ff4f67c98a046754cf804cf1d7e&sitexueshu_se 代码&#xff1a; https://github.com/junxiaosong/AlphaZero_Gomoku 理解&#xff1a; 0.应用领域&#xff1a;完美信息零和博弈&#xf…

算法学习(七)——一些基本概念:model base和model free,On-Policy 和 Off-Policy,On-Line 学习和 Off-Line 学习

model base和model free&#xff1a; model base指状态之间的转移关系确定&#xff0c;从而可以将决策问题转化成一个马尔科夫过程&#xff0c;从而用动态规划的方式得到最优解&#xff0c;使用条件比较苛刻。状态转移概率矩阵如下图&#xff1a; model free使用更加广泛&…

这段代码要在哪个空间里运行?js,vue,html都没试好

import { ref } from vueconst counter ref(0)console.log(counter) // { value: 0 } console.log(counter.value) // 0counter.value console.log(counter.value) // 1

为什么这个什么也不显示?

<div id"app"><post-item :post-title"title"></post-item> </div><script src"https://unpkg.com/vuenext"></script> <script> const app Vue.createApp({data() {return {title: Java无难事}} })…

算法学习(八)——SARSA和Q-Learning

SARSA是一种on-policy算法&#xff0c;Q-Learning是一种off-policy算法。 关于on-policy和off-policy的定义&#xff0c;网上有很多不同的讨论&#xff0c;我认为&#xff0c; on-policy和off-policy的差异 在于 训练目标策略 所用到的数据 &#xff08;有时候也表现为数据 &…

reactive()的使用,这短代码为什么不好使

<div id"app">count: {{ count }} </div><script src"https://unpkg.com/vuenext"></script> <script> const state Vue.reactive({count: 0 });const vm app.mount(#app) </script>为什么不出来count: 0呢&#xf…

127.0.0.1拒绝了我们的连接请求

问题描述 浏览器打不开127.0.0.1&#xff0c;显示拒绝连接。试着ping了一下。 需要指明一点&#xff0c;::1是ipv6的地址&#xff0c;但正常localhost应该指向127.0.0.1。 使用Windows自带的网络诊断工具&#xff0c;出现了“127.0.0.1未设置为接受端口“万维网服务(HTTP)”上…