promise的创建

news/2024/7/19 13:07:06 标签: js
const promise = new Promise(function(resolve, reject){
    setTimeout(function(){
        try {
            let c = 6 / 2 ;
            resolve(c);
            console.log(c)
        }catch(ex) {
            reject(ex);
            
        }
    }, 1000)
});


得出答案是3

但是把console.log放在最后一行会报错,说c没被定义

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


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

相关文章

Python3之max key参数学习记录

今天用Python写脚本,想要实现这样的功能:对于给定的字典,返回其中Value最大值对应的Key。 搜索后找到了解决方法,同时也学到了max key参数的作用。 例1, testlist [9.2, 10, -20.3, -7.0, 9.999, 20.111] print(ma…

promise的运行

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) })运行结果…

说有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;有时候也表现为数据 &…